(no) fast boolean evaluation ? missing NOT

Laurent Pointal laurent.pointal at limsi.fr
Fri Aug 3 11:57:03 CEST 2007


Stef Mientki a écrit :
> <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.

When using the *and* operator, the short-circuit evaluation is done if A 
is False (no need to know the other operand, the result cannot be True). 
But if A is True, the compiler must evaluate the second parameter to 
know the expression result.

[note: for the or operator, the short circuit is done if first operand 
is True]

A+

Laurent.

PS. See http://en.wikipedia.org/wiki/Truth_table or google for boolean 
logic tables.



More information about the Python-list mailing list