After a long discussion I’ve decided to add a shortcut conditional
expression to Python 2.5.
The syntax will be
1 | 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
1 2 3 | 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
1 2 3 4 | 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
lambda; currently
1 | 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.
Reference:https://mail.python.org/pipermail/python-dev/2005-September/056846.html
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.