(no) fast boolean evaluation ? missing NOT

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Fri Aug 3 05:45:17 EDT 2007


John Machin wrote:
> On Aug 3, 8:55 am, Ian Clark <icl... at mail.ewu.edu> wrote:
>> Stef Mientki 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
>> It's called short circuit evaluation and as far as I know it's standard
>> in most all languages. This only occurs if a conditional evaluates to
>> True and the only other operators that still need to be evaluated are
>> 'or's or the condition evaluates to False and all the other operators
>> are 'and's. The reason is those other operators will never change the
>> outcome: True or'd with any number of False's will still be True and
>> False and'ed to any number of Trues will still be False.
>>
>> My question would be why would you *not* want this?
>>
>>
> 
> Why? Perhaps under some compound condition like this:
> 
> (you_are_confused and/or
> function_returns_bool_but_has__side_effects())
> 

Thanks guys,
Yes this is exactly what's going wrong ...

Sorry, my question missed the essential "NOT",
here is an example, that behaves different in Delphi,
(so I guess Delphi is not a real language ;-)

<Python>
def Some_Function (const):
     print 'Ive been here', const
     return True

A = True

if A and Some_Function (4 ):
     print 'I knew it was True'
else:
     print 'I''ll never print this'
</Python>

<Output>
Ive been here 4
I knew it was True
</Output

I was expected that the function would not be called,
because A is True.

And I agree with Laurent that it should be better to write a clean code,
so it doesn't matter whether you write in Python or in Delphi.

Gabriel: you pointed me to this page:
The exact behavior is defined in the Language Reference <http://docs.python.org/ref/Booleans.html>

<quote>
or_test  	::=  	and_test | or_test "or" and_test
</quote

Can you imagine, while I'm not a programmer, just a human,
that I don't understand one bit of this line.

So now I'm left with just one question:
for bitwise operations I should use &, |, ^
for boolean operations I should use and, or, xor
but after doing some test I find strange effects:
 >>> A = 4
 >>> B = 5
 >>> A and B
5
 >>> A & B
4
 >>> A or B
4
 >>> A | B
5

So if I use the bitwise operation on integers,
   "and" changes into (bitwise) "or" and vise versa.

Is there some way to prevent / detect these kind of errors
( as I'm building a micro-controller simulator in Python,
I need both logical and bitwise operators very frequently).

cheers,
Stef Mientki





More information about the Python-list mailing list