PEP 308: Alternative conditional operator forms

David Gausebeck gausebec-spam at paypal.com
Mon Feb 10 00:35:02 EST 2003


>> <new keyword> C: x else y
>> 
>> and I like it, but no one else seems to have commented either way.
>> The original suggestion was for a keyword of 'ifelse' or possibly
>> 'select'.  I would prefer 'when' or 'case':
>> 
>> ifelse C: x else y
>> when C: x else y
>> case C: x else y
>>
>        I'd prefer to avoid overloading the : (which currently tends to not 
>only end a type of statement, but /also/ signals the expected start of 
>a code block -- even though a one-statement code block /can/ be put on 
>the same line). Same with using "else" and "if".

The reason the : makes sense for me is that the syntax here is
essentially the same as for 'if', except that this is an expression.
To make it even more analogous, it could be

ifelse C: x else: y

which, now that I look at it again, is what Paul Rubin actually
suggested.  I guess I dropped the second : because it's unnecessary.
But you do need something between C and x.

>        Assuming the creation of new keywords should be allowed...
>
>        when C then x otherwise y
>
>which may even allow for a form of multiple choice nesting...
>
>        when C1 then x when C2 then y ... otherwise z
>
>wherein the "otherwise" becomes the expression if none of the 
>"when/then" sets is taken.

Why add a new keyword ('otherwise') when an existing one fits
perfectly and causes no conflicts?  'else' in this context seems like
a no-brainer.

As for chaining, that could work in a couple ways depending on the
choice of keyword.  With ifelse, elif is the obvious choice:

ifelse C1: x elif C2: y else z

With case, you just continue to use case:

case C1: x case C2: y else z

Of course, whether chained conditionals are even desirable is a
subject of debate.  They can always be implemented via nesting,
however, and I'd say that

case C1: x case C2: y else z

is slightly clearer than

case C1: x else (case C2: y else z)

-Dave




More information about the Python-list mailing list