Proposed PEP for a Conditional Expression

Markus Schaber markus at schabi.de
Mon Sep 17 08:52:32 EDT 2001


Hi,

maxm <maxm at normik.dk> schrub:

> Besides ... what could be simpler than using the fact that booleans in
> Python is either 1 or 0:
> 
> #################################
>>>> a=3
>>>> b=4
>>>> print ('number two', 'number one')[a < b]
>>>> number one

This does not short-cirquit evaluate.

> or if you want to get the result of a functionwith only the choosen
> function evaluated:
> 
> #################################
>>>> def n1():
>>>>     return 'Number one'
>>>> def n2():
>>>>     return 'Number two'
>>>> print (n2, n1)[a < b]()
>>>> Number one
> 
> Any sane Python programmer will grasp those IMMEDIATELY!

That does short-cirquit, but has limits on the expressions you can use 
as n1 and n2 (may be that gamma and nested scopes help here).

markus

-- 
"The strength of the Constitution lies entirely in the determination of 
each citizen to defend it. Only if every single citizen feels duty 
bound to do his share in this defense are the constitutional rights 
secure." -- Albert Einstein



More information about the Python-list mailing list