[Python-Dev] Conditional Expression Resolution

Guido van Rossum guido at python.org
Fri Sep 30 03:21:53 CEST 2005

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
lambda; currently

    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.

It's still my language! :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list