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

Aahz aahz at pythoncraft.com
Thu Sep 29 13:43:21 CEST 2005

On Wed, Sep 21, 2005, Nick Coghlan wrote:
> Steven Bethard wrote:
>> Guido van Rossum wrote:
>>>I think I'd prefer (if <expr> then <expr> else <expre>) i.e. no
>>>colons. None of the other  expression forms (list comprehensions and
>>>generator expressions) involving statement keywords use colons.
>> FWIW, I find this quite intuitive.  It follows the same pattern as LCs
>> and GEs -- remove the colons and add parentheses (or brackets for
>> LCs).  So I'm +1.
> *But*, in LC's and GE's, the body of the main clause of the statement
> is also pulled out and placed in front of the keyword:
>    def gen():
>      for VAR in ITERABLE:
>        if COND:
>          yield EXPR
> becomes:
>    gen = (EXPR for VAR in ITERABLE if COND)
> This makes sense to me, because the most important thing in the
> generator expression is the way each element is populated - the source
> iterable and the filtering condition do matter, but they aren't as
> important.

Yes, and when the looping becomes the focal point, one should generally
rewrite a listcomp as a for loop.  Nevertheless, because boolean
expressions have non-boolean semantics in Python (i.e. they return the
objects rather than a boolean value), conditional expressions will
almost certainly have the condition as the focal point because a simple
boolean expression is no longer sufficient.  I am therefore strongly
opposed to the "a if cond else b" form.

(We've already agreed that Raymond's proposal to make boolean expressions
return booleans is dead, right?)

I'm also opposed to elif in conditional expressions -- let's keep this a
simple Pythonic rewrite of C's ternary.

I'm +0 on requiring parentheses precisely because they're annoying.  I'm
still expecting simple boolean expressions to be the primary use case,
and my hunch is that overall Python code will read better with the
ternary looking cluttered.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

The way to build large Python applications is to componentize and
loosely-couple the hell out of everything.

More information about the Python-Dev mailing list