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

Roy Smith roy at panix.com
Sun Feb 9 00:32:57 CET 2003


mis6 at pitt.edu (Michele Simionato) wrote:

> Evan <evan at 4-am.com> wrote in message 
> news:<1gb1a.33499$yb.1988258 at twister.austin.rr.com>...
> > Here's a totally new direction: let's get rid of the chaining and 
> > precedence issues, and make an n-way conditional, not a ternary.
> > 
> > {if cond1: expr1, default}
> > {if cond1: expr1, cond2: expr2, default}
> > etc.
> > 
> > Obviously, comma precedence with the braces works the same way as in a 
> > literal dict.
> > 
> > Examples based on the thread of discussion:
> > 
> > x = {if a < 0: 'negative', a > 0: 'positive', 'zero'}
> > 
> > if {if callable(obj): obj(), alttest()}:
> > 
> > roots = [{if x > 0, sqrt(x), '?'} for x in l]
> > 
> > Cheers,
> > 
> > Evan @ 4-am
> 
> Not convincing. For multiple choices one should use a dictionary. I am 
> favorable
> to a ternary operator but against a "case" operator.
> 
>                      M.

This is getting a bit off the original issue, but I disagree that 
dictionaries can (in general) be used instead of case statements.  
Certainly, a dictionary can be used to pick one of several values, but 
it can't be used to select one of several code paths.  I'm not arguing 
that Python needs a case statement, just pointing out that dictionaries 
and case are not interchangable.

Of course, the OOP fanatics might argue that if you're using a case 
statement to select code paths, you probably wanted a subclass instead, 
but that's a whole different flame-war :-)




More information about the Python-list mailing list