For review: PEP 308 - If-then-else expression
Andrew Koenig
ark at research.att.com
Sat Feb 8 09:51:52 EST 2003
Andrew> Hmmm, well, here's an ugly trick,
Andrew> col = [None, cols[max(i, 0)]][i>=0]
Yes, indeed, it's ugly -- especially because if i<=0 and len(cols)==0,
it fails where the original would have succeeded.
Andrew> and here's another
Andrew> col = [[None] + cols][max(i+1, 0)]
This one is O(len(cols)) where the original is O(1).
Andrew> but neither have the grace of
Andrew> col = None if i < 0 else cols[i]
Right.
Andrew> OTOH, I still say this is easier to debug, because I can do
Andrew> if i < 0:
Andrew> print "Negative?", i
Andrew> col = None
Andrew> else:
Andrew> print "i", i, "neighbors", cols[max(i-1, 0):i+1]
Andrew> col = cols[i]
Yes indeed. The flip side comes when you do something like this a bunch
of times and your function no longer fits on the screen.
--
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark
More information about the Python-list
mailing list