Conditional operator in Python?

Lloyd Hugh Allen vze2f978 at mail.verizon.net
Sun Apr 1 08:39:12 EDT 2001


Tim Peters wrote:
> 
> [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.

Umm...there's a good argument for using it in "Dive into Python"
<http://diveintopython.org/apihelper_andor.html>, that you can use
conditionals in a lambda where an <if...> is not allowed.

Made sense to me.

But then, I don't know much.



More information about the Python-list mailing list