Proposed PEP for a Conditional Expression

thp at cs.ucr.edu thp at cs.ucr.edu
Tue Sep 11 00:23:09 EDT 2001


Bernhard Herzog <bh at intevation.de> wrote:
[...]
: Another way that come to mind:

:         <expr> if <cond> else <expr>

: The order in which the condition and the braches occur is unusual for
: python, but this spelling has the advantage of not needing an additional
: keyword or colons.

I strongly favor the inclusion of a short-circuiting conditional
operator, and I think that the above syntax is probably the best
suggestion, since it closely resembles colloquial English and accepted
mathematical terminology for piecewise definitions, without
introducing new lexemes into the language.  E.g., the phrase

             "one if by land, two if by sea"

translates to 

              1 if by_lang() else 2 if by_sea() else 0

and definition

                       / <exp1> if <cond1>,
                      |  <exp2> if <cond2>,
                      /  <exp3> if <cond3>,
             f(x) =  <   <exp4> if <cond4>,
                      \  <exp5> if <cond5>,
                      |  <exp6> if <cond6>,
                       \ <exp7> otherwise

translates to 

             f = lambda x :
                   <exp1> if <cond1> else
                   <exp2> if <cond2> else
                   <exp3> if <cond3> else
                   <exp4> if <cond4> else
                   <exp5> if <cond5> else
                   <exp6> if <cond6> else
                   <exp7> 

Tom Payne





More information about the Python-list mailing list