Conditional operator in Python?

Tim Peters tim.one at home.com
Sun Apr 1 00:48:34 EST 2001


[Erik Max Francis]
> ...
> The Python FAQ, for instance, suggests x ? a : b can be reliably
> substituted with
>
>     (x and [a] or [b])[0]
>
> which definitely works ([a] and [b] are singleton lists and always
> evaluate true), but it makes the meaning unclear enough to defeat the
> purpose of wanting to use a conditional operator in the first place.

LOL!  I "invented" that "idiom" for Python in the early 90's, in a silly
thread where Steven Majewski and I tortured each other with the worst
constructs we could dream up.  If you ever see production code that actually
*uses* it, I encourage you to kill the author.

> ...
> On a vaguely related subject, why is there no Boolean type in Python?

Because Guido didn't feel one was needed.  Still doesn't, last I heard.
Barry Warsaw is a big fan of adding one, though, and since he got "print >>
file" in, there's no known lower limit on the uselessness of the ideas he can
sell <wink>.

next-up:-how-come-there-isn't-a-case-stmt?-ly y'rs  - endless tim





More information about the Python-list mailing list