C's ?: expression

Tim Peters tim_one at email.msn.com
Fri Apr 7 02:29:15 CEST 2000


[Curtis Jensen]
> I while back I posted about how to simulate C's ?: expression in
> Python.  There were quite a few responces to it.  Recently, I found on
> pp135-136 of Mark Lutz's "Programmin Python" book that there is a way.
> If anyone is interested:
>
> ((TEST and [trueAction]) or [falseAction])[0]
>
> where TEST is the boolean expression and tueAction is what to do if the
> expression is true and falseAction is what to do if the expression is
> false.

Yes, I invented that, and you could have found it more easily in the Python
FAQ (note that the inner parens aren't needed -- "and" binds tighter than
"or").  However, I have never used it in real code, and have been known to
kill people who do.  You really want to risk that, just to avoid an obvious
if/else <wink>?

better-programming-thru-intimidation-ly y'rs  - tim






More information about the Python-list mailing list