conditional expressions

Alex Martelli aleax at aleax.it
Fri Sep 27 09:47:09 CEST 2002


Pedro RODRIGUEZ wrote:
        ...
>> The tricks deal with ways to work around this problem, by ensuring that
>> and's RHO can never be false, e.g.
>>         zz = (a and [b] or [c])[0]
> 
> If python had short-circuiting operators ('and then', 'or else'), this
> will be the ternary operator :
>     zz = (a and then [b] or else [c])[0]

Python does have short-circuiting operators, and they're spelled 'and'
and 'or', exactly like I coded in the snippet you quote.


> The problem when talking about conditionnal expression should be to know
> what really matters in the context :
> - the short-circuit evaluation (only (a,b) or (a,c) are evaluated)
> - or the ability to pick a value in a list
> 
> The first one isn't possible in a Python expression AFAIK,

Of course it is -- you seem to keep ignoring the semantics of Python's
"and" and "or" operators, which ARE short-circuiting (and return one of
the operands, differently from C/C++'s short-circuiting && and ||,
which return 0 or 1 whatever operands they're given).

It's just an ugly hack, that's all.


Alex




More information about the Python-list mailing list