Erik Max Francis
max at alcyone.com
Mon Sep 23 02:41:45 EDT 2002
Bruce Dawson wrote:
> I find the (a and [b] or [c]) construct to obscure to be useful -
> space it saves is wasted by the comment I have to put in to explain
Precisely. The purpose of a conditional expression is to compress a
trivial if/else statement into an expression in a readable manner.
While you can use short-circuiting operators like and, or and lambda
expressions to get this effect, it defeats the readability purpose and
so is essentially useless. The shorter, more readable forms, tend to be
subject to corner cases having different behavior than a standard p ? a
: b conditional expression.
> It occurs to me that the bool() built in function allows for what is -
> in my opinion - a cleaner solution. Easier for mortals to comprehend.
Well, you could already use operator.truth for that.
> Basically, construct a two entry list of the two items and then index
> that with your expression, converted to bool. The bool function is
> essential since there was previously no generic way of getting the
> true/false value of an expression.
I use this technique frequently enough, but this isn't really what a
conditional expression does. A conditional expression only _evaluates_
one of its consequents, depending on the value of the test expression.
The (a, b)[operator.truth(p)] evaluates both.
This is fine in many cases, of course -- such as when you're selecting
between two strings -- but it isn't the full power of a true conditional
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ Many situations can be clarified by the passing of time.
\__/ Theodore Isaac Rubin
CSBuddy / http://www.alcyone.com/pyos/csbuddy/
A Counter-Strike server log file monitor in Python.
More information about the Python-list