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

Sean Ross frobozz_electric at hotmail.com
Fri Feb 7 16:33:53 EST 2003


>
>     The proposed syntax is as follows:
>
>         <expression1> if <condition> else <expression2>
>

While I do not think this is necessary, I would like to offer an alternative
syntax:

    <expression1> when <condition> else <expression2>

The only benefit I can see to this alternative syntax is that it will be
less likely to be confused with the existing syntax for if/elif/else, which
is probably a good thing. Although adding a keyword is often a bad thing...


>
>         x if C else y if D else z <==> x if C else (y if D else z)
>         x or y if C else z        <==> (x or y) if C else z
>         x if C else y or z        <==> x if C else (y or z)
>         lambda: x if C else y     <==> lambda: (x if C else y)
>         x if C else lambda: y     <==> SyntaxError
>         x if C else y, z          <==> (x if C else y), z
>         x, y if C else z          <==> x, (y if C else z)
>

consider:

x when C else y when D else z <==> x when C else (y when D else z)
x or y when C else z        <==> (x or y) when C else z
x when C else y or z        <==> x when C else (y or z)
lambda: x when C else y     <==> lambda: (x when C else y)
x when C else lambda: y     <==> SyntaxError
x when C else y, z          <==> (x when C else y), z
x, y when C else z          <==> x, (y when C else z)

and, in particular:

if x when C else y:
    pass

as opposed to:

if x if C else y:
    pass

Just a thought,
Sean Ross






More information about the Python-list mailing list