Another conditional expression candidate (PEP 308)

Evan evan at 4-am.com
Sun Feb 9 18:22:21 EST 2003


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.

The conditions and expressions appear in "natural" order -- the same 
order as in an 'if' statement, and the order in which they will be 
evaluated (short-circuiting aside).

No new keyword is introduced.  The grammar is unambiguous.  The syntax 
is utterly familiar and easy to remember, since it is "just like" the 
'if' statement, but in an expression context.

This has been proposed in the past (see 
http://groups.google.com/groups?threadm=mailman.1000046772.19777.python-list%40python.org), 
and I can't find any specific objections to it, other than someone 
uncomfortable with the idea of mandatory parentheses.

Cheers,

Evan @ 4-am





More information about the Python-list mailing list