For review: PEP 308 - If-then-else expression
Andrew Dalke
adalke at mindspring.com
Sat Feb 8 13:11:15 EST 2003
Erik Max Francis
> If I see
>
> 's'[:count != 1]
>
> I have to sit and think.
True. In my code I would have made a temp variable and not worried
about the couple extra lines of code. Or I would have made that function.
> My example was deliberately simple, to which there are (still in my
> opinion) less clear alternatives. For _real_ short circuiting, if you
> want that compressed into an expression you have some real IOPyCC
> contenders:
As I pointed out elsewhere, the ternary operator is only rarely used to
prevent side-effects. In most cases I've seen, there are no differences
with or without short circuiting.
In other words, how often will short circuiting be needed in real code?
David Eppstein pointed out one, with
if i < 0:
col = None
else:
col = cols[i]
which is nicely expressed as
col = cols[i] if i < 0 else None
Otherwise, I feel that most real-life problems are more naturally in other
Python idioms.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list