[Python-Dev] Adding a conditional expression in Py3.0

Jim Jewett jimjjewett at gmail.com
Wed Sep 21 21:14:34 CEST 2005

Ron Adam wrote:

> ( e selects (e1 selects a1, b1),
>                   (e2 selects a2, b2),
>                   (e3 selects a3, b3) )

I think you've just reinvented the case statement, which
disagrees with "if" over the order of true and false clauses.

For a conditional expression, I think the choices are really down
to the following, which was already way too much freedom last
(http://www.python.org/peps/pep-0308.html) time:

(1)  Should it be done at all?
    +  It would be useful, and avoid certain types of bugs.
    -  It would encourage bracketing instead of indentation

PEP 308 decided "not yet, anyhow"

(2)  How many of the keywords (if ... then ... else ... elif) should be dropped?
    (if test then True else False)
        + standard english
        + standard programming idiom
        - adds a "then" keyword
    (if test True else False)
        + better parallels the if: statement
        - starts to run together
    (if test1 Val1 elif test2 Val2 elif test3 Val3)
        + parallels the if: statement
        - encourages overly long code
        * but still better than nested parens

PEP 308 wasn't entirely clear; the words said to keep "elif", but
the examples did not.  There was some disagreement on which
of the other three keywords to represent explicitly.  (Rather than
only by position.)

(3)  What order should the clauses appear?
    (if test then True else False)
    (if test1 then Val1 elif test2 Val2 elif test3 Val3 else Val4)
        + Natural Order
        - do we need "then"?
    (True if normal else False)
    (Val1 if test1 else Val2 if test2 else Val3 if test3 else Val4)
        + Best order for normal/default conditionals
        + Best order in general if we weren't used to left-right processing
        - But we *do* expect left->right almost everywhere except assignments

PEP 308 made it clear that "else" should be last.  Putting the condition
before the "then" was not as clearcut.

(4)  What sort of punctuation should augment or replace the keywords?

PEP 308 suggested avoiding punctuation, but it wasn't clearcut.


More information about the Python-Dev mailing list