Re: (no) fast boolean evaluation ?



On Aug 2, 10:47 pm, Stef Mientki <S.Mientki-nos...@xxxxxxxxxxxxxx>
wrote:
hello,

I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

Is this standard behavior or is there a compiler switch to turn it on/off ?

thanks,
Stef Mientki

The following program shows a(clumsy)? way to defeat the short-
circuiting:


def f(x):
print "f(%s)=%s" % ('x',x),
return x
def g(x):
print "g(%s)=%s" % ('x',x),
return x

print "\nShort circuit"
for i in (True, False):
for j in (True, False):
print i,j,":", f(i) and g(j)

print "\nShort circuit defeated"
for i in (True, False):
for j in (True, False):
print i,j,":", g(j) if f(i) else (g(j) and False)


The output is:

Short circuit
True True : f(x)=True g(x)=True True
True False : f(x)=True g(x)=False False
False True : f(x)=False False
False False : f(x)=False False

Short circuit defeated
True True : f(x)=True g(x)=True True
True False : f(x)=True g(x)=False False
False True : f(x)=False g(x)=True False
False False : f(x)=False g(x)=False False


- Paddy.


.



Relevant Pages

  • Re: Simulating simple electric circuits
    ... class CurrentController which does the main work ... A relay "ZT" is brought up if an outside key is ... I don't have access to all circuit diagrams. ... def newSource: ...
    (comp.lang.python)
  • Re: (no) fast boolean evaluation ?
    ... Stef Mientki ... way to defeat the short- ... def f: ... print "\nShort circuit defeated" ...
    (comp.lang.python)
  • Re: (no) fast boolean evaluation ?
    ... Stef Mientki ... def g: ... print "\nShort circuit defeated: OR" ... Gosh That port last night was good;-) ...
    (comp.lang.python)