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

Terry Reedy tjreedy at udel.edu
Sat Sep 24 04:19:48 CEST 2005

"Jim Jewett" <jimjjewett at gmail.com> wrote in message 
news:fb6fbf5605092112145cfda669 at mail.gmail.com...

A nice summary, to which I will add just a little.

> 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

I think the first form with 'then' better parallels the 'if <cond> :' 
statement because 'then' functions as a separator between test and True 
just as ':' or ':\n' functions as a separator between <cond> and <True 

>    (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

I an far from convinced on either of these, perhaps because I am not sure 
about the actual claim.

>        - But we *do* expect left->right almost everywhere except 
> assignments

          - Ambiguous.

When expressions a and b both lack boolean operators, as is common in 
Python given that all objects have a boolean value, a if b else c is 
mentally parseable as either (a if) b (else c) or a (if b) (else c).  I 
initially though the proposal intended the first.

         -- Bug prone.  The left->right expectation leads to the wrong 

Hence we may expect occasional bugs both in writing and reading this 

> 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.

Terry J. Reedy

More information about the Python-Dev mailing list