Update to PEP308: if-then-else expression
Gerald S. Williams
gsw at agere.com
Tue Feb 11 16:59:17 CET 2003
Raymond Hettinger wrote:
> I updated the PEP for Guido van Rossum.
Great job! Thanks.
Two things you may consider adding:
> * (if <condition>: <expression1> else: <condition2>) is in vogue.
There was some discussion about allowing elif: in this context, which
is a natural thing to consider (at least as an option).
The current alternatives shown in the PEP and in FAQ section 4.16
all have serious drawbacks, as both are quick to point out. A more
workable alternative using existing constructs could be shown. I
propose showing a variant of the Tim Peters alternative (feel free
to rename ifelse() into cond(), iif(), or whatever you please):
def ifelse(x): return [x]
... ifelse(cond and [true-expr] or [false-expr])
Note that this is an n-ary operation:
... ifelse(a and [x] or b and [y] or c and [z] or ...)
I'm not against adding ternary (or n-ary) operations, but existing
alternatives should really be given fair treatment.
More information about the Python-list