For review: PEP 308 - If-then-else expression
David Gausebeck
gausebec-spam at paypal.com
Fri Feb 7 18:16:53 EST 2003
>Adding a keyword doesn't seem to me like a big problem, unless it's a
>keyword that will break a lot of programs (e.g. don't use "n" as a
>keyword). So here's another alternative:
>
> ifelse condition: expression1 else: expression2
>
>or if the word "ifelse" is too ugly, we could use a different one instead,
>like maybe "select".
>
>The else clause could be chainable with elif:
>
> ifelse cond1: exp1 elif cond2: exp2 else: exp3
So this would be exactly like the normal if syntax, except it's an
expression instead of a statement. This seems like a pretty
good/clean way to handle it. Also very easy to explain.
It would also be easy to allow defaulting to None. i.e.
ifelse condition: expression1
is equivalent to
ifelse condition: expression1 else: None
I agree that it would benefit from a prettier keyword, though. I
can't think of anything particularly good... "cond" or "decode" are a
couple more possibilities, though.
-Dave
More information about the Python-list
mailing list