conditional expressions
Pedro RODRIGUEZ
pedro_rodriguez at club-internet.fr
Thu Sep 26 14:36:40 EDT 2002
On Thu, 26 Sep 2002 19:22:11 +0200, Alex Martelli wrote:
> Pedro RODRIGUEZ wrote:
> ...
>>>> But why use grotesque hacks in the first place? "a ? b : c" is a
> ...
>> Ada has two short-circuits operators : "and then" and "or else" that do
>> the trick :
>> if A and then B then
>> ...
>> end if;
>>
>> Looks cleaner to me.
>
> Ada's "and then" is like Python's "and"; Ada;s "or else" is like
> Python's "or". Neither is like C's "a?b:c" (ternary operator), though
> with some tricks boolean shortcircuiting operators can be used to
> simulate ternary operators (a bit cumbersome in the general case).
>
> The reason
> zz = a and b or c
> is not the same thing as
> zz = a ? b : c
> is that, when a is true and b is false, the former returns c, while the
> latter returns b.
>
I stand corrected the value of b should not be part of the condition but
only of the result.
> 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]
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,
and the second one can be expressed as (b,c)[not a].
Pedro
More information about the Python-list
mailing list