Proposed PEP for a Conditional Expression

Greg Ewing greg at
Thu Sep 13 02:47:53 CEST 2001

Bernhard Herzog <bh at> wrote:

>         <expr> if <cond> else <expr>

I find myself liking this. To me,

  x = a if something else b

reads much more smoothly than

  x = if something then a else b

Rainer Deyke wrote:
> However, it would not fit well with list comprehensions that have a 'for':
> [<expr> if <cond> for i in range(5)] # Syntax error
> [<expr> for i in range(5) if <cond> else <expr>] # Syntax error
>                                          # and ambiguous intent

This can be remedied: give the conditional expression
a precedence lower than any existing operator (which I
think is a good idea anyway), and exclude this level of 
precedence from the iterated-over expression of a for loop.

"Terry Reedy" <tjreedy at>:

> I agree that the above is backwards.
> <cond> if <expr> else <expr>

No! That sounds way too much like Forth!

Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:

More information about the Python-list mailing list