Proposed PEP for a Conditional Expression

Gareth McCaughan Gareth.McCaughan at
Mon Sep 10 10:49:51 CEST 2001

Michael Chermside wrote:
[SNIP: most of an excellent proto-PEP; I've preserved only
bits I want to quibble about.]

>          WORKAROUND_3: (this is the most common idiom)
>              > x = (c and [a] or [b])[0]
>              Here we create one-element lists and index into them.
>              Since [a] is definitely NOT false (it's got 1 element),
>              the and-or syntax will succeed.
>          WORKAROUND_4: (lambda also works)
>              > x = (c and (lambda:a) or (lambda:b))()
>              This is equivalent to WORKAROUND_3, but most people
>              consider it to be slightly less readable, particularly
>              since conditional statements are often used in lambda
>              expressions, thus complicating things further.

This is not equivalent to WORKAROUND_3. It doesn't do the right
thing unless nested scopes are turned on.

    def f(x):
      return (x==0 and (lambda:1) or (lambda:x))()

generates a SyntaxWarning when compiled and a NameError when
used, under 2.1 .

>      ARG_21: Simplifies lambda expressions.
>          Lambda expressions allow one to create an annonomous function


>      Arguments for or against HAVING a conditional expression are
>      found in the Rationale section, the arguments here simply deal
>      with different means of

... stopping in mid-

Gareth McCaughan  Gareth.McCaughan at
.sig under construc

More information about the Python-list mailing list