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

Guido van Rossum guido at python.org
Fri Sep 30 01:18:29 CEST 2005

> > Let me give you what you expect. If all the "X if C else Y" syntax
> > does is prevent that atrocity from ever being introduced, it would be
> > worth it. :)

> Well, fine. However, it does allow atrocities like
> func(f for f in lst if f > -1 if f < 0 else +1)

No it doesn't! Inside an 'if' (of any flavor), further ifs have to be
nested. So you'd have to write

  func(f for f in lst if f > (-1 if f < 0 else +1))

or perhaps

  func(f for f in lst if (f > -1 if f < 0 else +1))

But I doubt you meant to write +1 where True could have sufficed. :)

An if-else expression has lower priority than anything else except
lambda; the expression

lambda x: x if x >= 0 else -x

is equivalent to

lambda x: (x if x >= 0 else -x)

> I realise that any chosen syntax is subject to abuse, but a conditional
> expression in a (currently allowed) conditional context will be
> guaranteed obscure. Your original instinct to omit conditional
> expressions was right!

Now you've pushed me over the edge. I've made up my mind now, "X if C
else Y" it will be. I hope to find time to implement it in Python 2.5.
Let it be as controversial as bool or @decorator, I don't care.

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

More information about the Python-Dev mailing list