For review: PEP 308 - If-then-else expression
Andrew Dalke
adalke at mindspring.com
Sat Feb 8 03:51:28 EST 2003
David Eppstein:
> As far as I can remember, the last time I wanted to use that construct,
> I couldn't, because b was false. Hah! Found it:
>
> if i < 0:
> col = None
> else:
> col = cols[i]
Ooo, I like this example. The tricks I've used elsewhere don't work here,
so this is indeed one where the if/else expression would make things clearer
and where short-circuit evaluation is needed and appropriate.
Hmmm, well, here's an ugly trick,
col = [None, cols[max(i, 0)]][i>=0]
and here's another
col = [[None] + cols][max(i+1, 0)]
but neither have the grace of
col = None if i < 0 else cols[i]
OTOH, I still say this is easier to debug, because I can do
if i < 0:
print "Negative?", i
col = None
else:
print "i", i, "neighbors", cols[max(i-1, 0):i+1]
col = cols[i]
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list