Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   AW: Ternery operator (http://www.velocityreviews.com/forums/t322244-aw-ternery-operator.html)

 Uwe Schmitt 09-09-2003 12:43 PM

AW: Ternery operator

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

 All times are GMT. The time now is 02:05 PM.