Brian Ray's BlogPainting is just another way of keeping a diary. --Picasso
Fri, 20 Jan 2006
Python Conditional Expressions
In a rather silly Chicago Python User Group Mailing List Thread, it was brought to my attention Python 2.5 will have a not-so-silly new in-line Conditional Expression feature. Ok, so I have spent a lot of time making fun of (and sometimes secretly coding, but don't tell anybody) languages where programmers spend hours trying to write everything on one line. Likewise, I found myself hacking away on the recent seven-segment PyContest where we attempted to solve this puzzle by writing Python on one line and with a ridiculously few number of keystrokes (like 117). Would I support yet another one-liner time saver syntax much like decorators, list compression, or the lambda construct? Yes, actually, in-line conditional expressions are a worth while language addition. The reason i am found of this feature goes back to the PEP. This ternary expression (at least the one accepted by BDFL), is extraordinarily easy to read in the form: X if C else Y So, now things can be done like (factored from dev-list and ChiPy mailing list): return TRUE if a else FALSE l = map(lambda x:'%s: "%s"' % (x[0], '""'.join(x[1]) if x[1][0] else ''), l) b = if a[i] else a 1 if z in [1,'a',7,9] else 2 Of course, I have no clue what I just typed would actually work. That's not my point. The point is that I get some sort of understanding of each lines function by simply glancing at it. In other words, if I am reading through 1200 lines of code, this line would not throw me off so much as with one-liners from other languages, say C or PERL. I usually wait until new language features are actually implemented and, usually, for them to appear in other sources before I even consider. For example, I am just now starting to use decorators. I trust the PSF and (in this case) BDFL who made the call on this one. I am excited about writing one-liners with this one, but only in Python.
|
Categories Calendar
Check out my new Blog -> |
|||||||||||||||||||||||||||||||||||||||||||||||||