For review: PEP 308 - If-then-else expression
Chermside, Michael
mchermside at ingdirect.com
Fri Feb 28 08:25:18 EST 2003
Janto writes:
> Geoff> What about ifelse(condition, then clause[, else clause])?
>
> I find it difficult to imagine a situation where the short-circuit
> behavior actually matters. Theoretically an ifelse function doesn't do
> the same thing, but practically it does: Whenever I've used ternary
> ops there weren't any side effects to evaluating both arguments.
> Neither any performance ones.
>
> Can someone give me a real world example of its usefulness?
Simple example:
print ifelse(y==0, x/y, "undefined")
In fact, it is QUITE COMMON to use a conditional expression as a
"guard"... checking for a special case in which the general formula
would break. Another example would be:
y = ifelse( x is None, None, func(x) )
Either of these would, of course, not work without short-circuiting /
lazy evaluation.
-- Michael Chermside
More information about the Python-list
mailing list