[Python-Dev] Adding a conditional expression in Py3.0
Steve Holden
steve at holdenweb.com
Fri Sep 30 08:54:46 CEST 2005
Guido van Rossum wrote:
> [Guido]
>
>>>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. :)
>
>
> [Steve]
>
>>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. :)
>
:)
All that said, inside an "if" is hardly the best place for a conditional
of any kind. Clearly such usage can go down as abuse.
> 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)
>
That's about the only way it would make sense, I suppose.
>
>>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.
>
Not before time, either. If this ends the discussion then I'll consider
I've done everyone a favour. Sometimes no decision is worse than the
wrong decision ;-).
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-Dev
mailing list