Proposed PEP for a Conditional Expression

Michael Chermside mcherm at destiny.com
Fri Sep 14 16:29:59 EDT 2001


James_Althoff writes:
> With apologies for possibly having missed some parts of this thread . . .
> 
> Given:
> 1) nested scopes, and
> 2) recent proposals becoming more and more complicated and convoluted --
> e.g., embedding "break" and "return" statements inside if:else: expression
> syntax, highly overloaded meanings of if:else: ,
> inside/outside/upside/downside ordering of if:else: keywords, etc., . . .
> 
> Why not just start with something simple like a builtin "cond" function
> approximating the following:
> 
> 

I *think* I can answer your question by quoting from the PEP:

>         WORKAROUND_1: (this DOESN'T WORK!)
>             > def cond(c, a, b):
>             >     if c: return a
>             >     else: return b
>             > x = cond(c, a, b)
>             This fails, because it does not short-circuit... the
>             arguments the the function are always evaluated before
>             the function is invoked. Thus, for instance, the
>             following would not work properly when a == 0:
>             > x = cond( a==0, DEFAULT_VAL, 100/a )

Do you see why the "cond" solution won't do?

-- Michael Chermside







More information about the Python-list mailing list