AW: Ternery operator

U

Uwe Schmitt

:: 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").

The second one does short circuit evaluation, try the following:


def T():
print "T called"
return "T"
def F():
print "F called"
return "F"

res1 = (1 and [T()] or [F()])[0]
res2 = (0 and [T()] or [F()])[0]

Greetings, Uwe.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top