Another conditional expression candidate (PEP 308)

Roy Smith roy at panix.com
Sun Feb 9 18:57:18 EST 2003


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.

Why is the else: manditory?  Why not have it return None if none of the 
conditions are true?  It seems natural, and makes the syntax even more 
like the current if statement.  Other than that, I think I like it.  I 
certainly like it better than any of the other alternatives I've seen.

The next step (I'm being deliberately subversive here) is to say that 
enclosing any statement in ()'s turns it into an expression whose value 
is the last thing evaluated in the statement.  That would let you do 
things like:

while (x = foo()):




More information about the Python-list mailing list