For review: PEP 308 - If-then-else expression
Anders J. Munch
andersjm at inbound.dk
Sat Feb 8 19:02:57 CET 2003
> Guido van Rossum <guido at python.org> writes:
> > The proposed syntax is as follows:
> > <expression1> if <condition> else <expression2>
The order of evaluation of this is middle-then-left-or-right.
Also, I keep misreading "A if B else C" as
<condition> if <if-value> else <else-value>
-0.5 on this specific syntax.
PEP> if <condition> : <expression1> else: <expression2>
PEP> is even more confusing because it resembles the if statement so
Ah, but I have a fix in store for that from the last time this was
discussed: Require parens around the expression.
Add to the grammar:
if_expr ( "," if_expr )* [","]
"if" comparison ":" if_expr "else" ":" if_expr
and replace expression_list with if_expr_list in parenth_form, and
perhaps also in string_conversion and subscription, but not in
assignment_stmt, return_stmt, yield_stmt etc.
x = (if A: B else: C) # legal
x[if A: B else: C] = D # perhaps legal, or perhaps you need
x[(if A: B else: C)] = D # legal
print `if A: B else: C` # perhaps legal
f(if A: B else: C, D) # legal, D is the second argument to f
x = if A: B else: C # ILLEGAL
if if A: B else: C: # VERY ILLEGAL
if (if A: B else: C): # legal
I think requiring parens like this would be a good idea with all
syntaxes, lest we want to see
if A if B if C else D else E:
More information about the Python-list