For review: PEP 308 - If-then-else expression

holger krekel pyth at devel.trillke.net
Fri Feb 7 12:46:55 EST 2003


Guido van Rossum wrote:
> Given that if-then-else expressions keep being requested, I hereby put
> forward a proposal.  I am neutral on acceptance of the proposal; I'd
> like the c.l.py community to accept or reject it or come up with a
> counter-proposal.  Please understand that I don't have a lot of time
> to moderate the discussion; I'll just wait for the community to
> discuss the proposal and agree on some way to count votes, then count
> them.

Nice overall procedure! Although i vote against discussing voting schemes :-)
Let's call for votes at some later point next week.  No need to hurry. 

I suspect you know the outcome already as we all know you can't
restrain yourself from using that pesky little time machine. 

> Proposal
> 
>     The proposed syntax is as follows:
> 
>         <expression1> if <condition> else <expression2>
> 
>     This is evaluated like this:
> 
>     - First, <condition> is evaluated.
> 
>     - If <condition> is true, <expression1> is evaluated and is the
>       result of the whole thing.
> 
>     - If <condition> is false, <expression2> is evaluated and is the
>       result of the whole thing.
> 
>     Note that at most one of <expression1> and <expression2> is
>     evaluated.

What about a missing else clause? e.g. 

        <expression> if <condition>

Allowing it could be nice for stuff like 

    result = obj(...) if callable(obj)

Note though, that this proposal obfuscates control "flow"  
(which guido already knows, of course).  You can't read it 
neither from "left to right" nor from "right to left".  

It has happenend with list comprehensions before. But this doesn't 
mean it's generally a good thing.  For example consider

    if obj() if callable(obj) else default:
       ...

where it's not immeditiately obvious what happens as 
keywords loose significance.  And this is just the start 
of oneliners i could and would come up with :-)

hoping for some good discussions. 

regards,

    holger





More information about the Python-list mailing list