Another conditional expression candidate (PEP 308)

Evan evan at 4-am.com
Mon Feb 10 15:08:49 CET 2003


Andrew Koenig wrote:
> I don't see any reason for the parens to be mandatory.

It eliminates the Corner Case, precedence issues, and Alan Daniels 
objection elsewhere in this thread.

> In particular, I don't see any reason why I should not be
> able to write
> 
>         foo(if a > 0: 'positive' elif a == 0: 'zero' else: 'negative')

Now add an operator to the right end of the expression.  Sure, it is 
possible to set up precedence so that it usually "does the right thing", 
but an inexperienced programmer, or one who simply prefers not to use 
conditional expressions in their own code, will have to stop and think 
when they read in someone else's code:

foo(if a > 0: 'positive' elif a == 0: 'zero' else: 'negative', 1)

Is that a tuple or a pair of arguments?  On the other hand,

foo((if a > 0: 'positive' elif a == 0: 'zero' else: 'negative'), 1)

See?  Also, parse this for me (assuming that those who also want else: 
to be optional win):

x = if c1: e1 elif c2: if c3: e3 else: e4

Now parse this:

x = (if c1: e1 elif c2: (if c3: e3) else: e4)

My point is that if Guido is going to agree to add this controversial 
new feature to Python, the result should be absolutely 
no-thought-required readable and consistent.  Nothing optional, apart 
from the inherent n-way optionality of the elif: clauses.  No precedence 
questions.  No Corner Case.  Dead simple.

Cheers,

Evan @ 4-am





More information about the Python-list mailing list