Python paradigms

Robert W. Cunningham rcunning at acm.org
Sun Apr 9 00:38:22 EDT 2000


David Goodger wrote:

> C's ?: operator, ie:
>
>     test ? true : false
>
> can be done in Python, with:
>
>     ((test and [true]) or [false])[0]
>
> Note that I said *can* be done, not *should* be done! It is very ugly and
> counter-intuitive and naughty. But sometimes necessary. Use sparingly,
> especially if your code will ever be read (even by you!).

As another Python newbie, I just came across this usage in some random
Python code I downloaded (so I could get a look at some "real world" Python
programming).  All the author did was something like this:

 blah = (((test and [true]) or [false])[0])             # blah = (test ?
true : false)

Works for me!  Only if I have a good reason to suspect a problem in this
particular line of code will I have to look past the comment.  Which, I
believe, is the entire purpose of in-line comments in the first place.  The
best way to get someone to read every single line of your code is to fail to
properly comment it.

Are there ANY Python programmers who don't also know at least this much C?

You ALL use in-line comments, don't you?  Though, in this particular case, I
suspect an apology should be added as well.

But all this can be avoided, of course, with judicious use of m4 or sed.
After all, shouldn't Python have a pre-processor too?  A pre-interpreter
interpreter?  Or, maybe, I should just stop learning Python and use C2Py
instead.  If, of course, such a beast were to exist.

All for the love of a nice big juicy side effect...


Lacking-words-for-a-better-closing-ly y'rs,

-BobC





More information about the Python-list mailing list