Velocity Reviews > RE: Ternery operator

# RE: Ternery operator

Michael Chermside
Guest
Posts: n/a

 09-09-2003
Uwe Schmitt writes:
> Normaly[sic] you should simulate "C ? T : F"
>
> either by
> [T,F][not C]
>
> or
>
> (C and [T] or [F])[0]
>
> in the first case T and F are evaluated[sic] allways[sic],
> the latter solution does short circuit evaluation,
> which is according to the C/C++ semantics of the
> ternary operator.

NOT TRUE!

NEITHER of your options does short-circuit evaluation. That's
part of why many people (myself included) wanted to have a
conditional expression (more accurate term than "ternary operator").
If you want short-circuit evaluation (and sometimes you DO need
it), then use this:

if C:
x = T
else:
x = F

If you are writing a lambda expression, or are in some other location
where an expression would be simpler and clearer than a 4-line
statement, then too bad. There DO exist alternatives, but they are
even more obscure than Uwe's examples above, and should NOT be used
unless you are writing an obfuscated Python entry.

-- Michael Chermside

Andrew Koenig
Guest
Posts: n/a

 09-09-2003
Michael> Uwe Schmitt writes:

>> Normaly[sic] you should simulate "C ? T : F"

>
>> either by
>> [T,F][not C]

>
>> or

>
>> (C and [T] or [F])[0]

>
>> in the first case T and F are evaluated[sic] allways[sic],
>> the latter solution does short circuit evaluation,
>> which is according to the C/C++ semantics of the
>> ternary operator.

Michael> NOT TRUE!

Michael> NEITHER of your options does short-circuit evaluation. That's
Michael> part of why many people (myself included) wanted to have a
Michael> conditional expression (more accurate term than "ternary
Michael> operator").

NOT TRUE! The second option does short-circuit evaluation:

(C and [T] or [F])[0]

--
Andrew Koenig, http://www.velocityreviews.com/forums/(E-Mail Removed)

Erik Max Francis
Guest
Posts: n/a

 09-09-2003
Michael Chermside wrote:

> Uwe Schmitt writes:
>
> > Normaly[sic] you should simulate "C ? T : F"
> >
> > either by
> > [T,F][not C]
> >
> > or
> >
> > (C and [T] or [F])[0]
> > ...

>
> NOT TRUE!
>
> NEITHER of your options does short-circuit evaluation.

The latter does, due to its use of and/or. An equivalent form including
lambdas is

(C and lambda: T or lambda: F)()

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Silence is the most perfect expression of scorn.
\__/ George Bernard Shaw