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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Alex Vinokur C++ 4 11-26-2004 11:46 PM John Smith C++ 2 10-06-2004 10:22 AM Jakob Bieling C++ 2 03-05-2004 04:27 PM Andrew Chalk Python 15 09-09-2003 03:27 PM Uwe Schmitt Python 0 09-09-2003 12:43 PM