After a long discussion I’ve decided to add a shortcut conditional
expression to Python 2.5.
The syntax will be
A if C else B
This first evaluates C; if it is true, A is evaluated to give the
result, otherwise, B is evaluated to give the result.
The priorities will be such that you can write
x = A if C else B
x = lambda: A if C else B
x = A if C else B if D else E
But you’d have to write
if (A if C else B):
[x for x in seq if (A if C else B)]
A if (X if C else Y) else B
(A if C else B) if D else E
Note that all these are intentionally ugly. 🙂
In general, ‘if’ and ‘else’ bind less tight than everything except lambda.
We will adjust the syntax of what goes inside an ‘if’ to disallow
if lambda: x:
is accepted but quite useless (it’s always true) so this will be disallowed.
Flames, pleas to reconsider, etc., to /dev/null.
Congratulations gracefully accepted.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.