Re:Python idioms I wish I'd learned earlier
by rectangletangle on Thursday November 27, 2014 @03:40AM
(#8666457)
I'm a fan of Python's conditional expressions.
They're particularly interesting when combined with comprehensions.
Though this particular example can be expressed much more concisely.
[ Parent
| on HN ]
Re:Python idioms I wish I'd learned earlier
by plikan13 on Thursday November 27, 2014 @07:11AM
(#8666889)
Before the conditional expression was introduced to Python I sometimes wrote things like
foo = [baz, bar][qux is None]
Re:Python idioms I wish I'd learned earlier
by TillE on Thursday November 27, 2014 @06:45AM
(#8666828)
That's a Pythonization of C's conditional operator.
foo = qux == NULL ? bar : baz;
Of course, C does not do list comprehensions.
Re:Python idioms I wish I'd learned earlier
by misiti3780 on Thursday November 27, 2014 @01:11PM
(#8668302)
i didnt realize you could add the if statement in the comprehension where you did
['a' if i % 2 == 0 else 'b' for i in range(10)]
very cool!I was only aware of code like
['a' for i in range(10) if i % 2 == 0]
Re:Python idioms I wish I'd learned earlier
by misiti3780 on Thursday November 27, 2014 @01:10PM
(#8668294)
i didnt realize you could add the if statement in the comprehension where you did
['a' if i % 2 == 0 else 'b' for i in range(10)]
very cool!I was on aware of code like
['a' for i in range(10) if i % 2 == 0]