For review: PEP 308 - If-then-else expression

sismex01 at hebmex.com sismex01 at hebmex.com
Fri Feb 28 10:06:29 EST 2003


> From: Chermside, Michael [mailto:mchermside at ingdirect.com]
> Sent: Friday, February 28, 2003 7:25 AM
> 
> 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
>

I've *had* to use VBasic to work on some stuff, and it has an iff()
function, kinda like your ifelse().  It's utterly useless.  The reason
it's useless is because it doesn't short-circuit.

So your example above, in VB, would be:

   q = iff(y <> 0, x/y, 0)

would fail miserably, because x/y would be evaluated even if y==0,
which is NOT aparent upon reading the function description.

:-/

Another strike agains M$ language designers. :-P

-gus





More information about the Python-list mailing list