Another conditional expression candidate (PEP 308)

Alan Daniels from_usenet at alandaniels.com
Sun Feb 9 23:48:35 EST 2003


On Sun, 09 Feb 2003 23:22:21 GMT, Evan <evan at 4-am.com> wrote:

>x = (if a > 0: 'positive' elif a == 0: 'zero' else: 'negative')
>
>The parentheses and 'else:' clause are mandatory.  Leave them off and 
>you get a SyntaxError.

Am I missing something? The whole point of a ternary operator is to
save you extra typing. This just seems to take everything you'd
typically write and squish it into one line.

Also, I think this would also be VERY confusing to new users.
This would be legal syntax:

    result = if cond: x else: y

But this...

    result = if cond:
        x
    else:
        y

...would be illegal (it certainly is *now*, anyway). So to implement
this would require changing the fact that "if" blocks are not
expressions. Do I understand this correctly? Thanks.





More information about the Python-list mailing list