Proposed PEP for a Conditional Expression

Rainer Deyke root at rainerdeyke.com
Thu Sep 13 01:31:44 EDT 2001


"Greg Ewing" <greg at cosc.canterbury.ac.nz> wrote in message
news:3BA00239.DECE6412 at cosc.canterbury.ac.nz...
> Bernhard Herzog <bh at intevation.de> 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.

I was actually considering the following interpretations:

[(<expr> if <cond> else <expr>) for i in range(5)]
[(<expr> for i in range(5)) if <cond> else <expr>]

It seems you were thinking of one that never even occurred to me:

[<expr> for i in (range(5) if <cond> else <expr>)]


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list