(no) fast boolean evaluation ?

S

Stef Mientki

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
 
G

Gabriel Genellina

En Thu, 02 Aug 2007 18:47:49 -0300, Stef Mientki
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 ?

The exact behavior is defined in the Language Reference
<http://docs.python.org/ref/Booleans.html>

"The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is
returned; otherwise, y is evaluated and the resulting value is returned.
Note that neither and nor or restrict the value and type they return to
False and True, but rather return the last evaluated argument. This is
sometimes useful, e.g., if s is a string that should be replaced by a
default value if it is empty, the expression s or 'foo' yields the desired
value."

Tutorial section 5.7 say the same thing in a colloquial way.
 
I

Ian Clark

Stef said:
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?

Ian
 
E

Evan Klitzke

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 ?

This is standard behavior in every language I've ever encountered. If
you are evaluating an and/or with side effects and you need both side
effects to occur, you can trivially write functions implementing this
behavior, e.g.

def a():
print 'foo'
def b():
print 'bar'
def my_and(lh, rh):
return a and b

Then my_and(a(), b()) will evaluate both a and b and print both foo
and bar even though a() is False.
 
J

John Machin

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())
 
J

John Machin

This is standard behavior in every language I've ever encountered. If
you are evaluating an and/or with side effects and you need both side
effects to occur, you can trivially write functions implementing this
behavior, e.g.

If each operand is of type bool (or, more generally,
isinstance(operand, int) is true), you could trivially use the & and |
operators.
 
J

Joshua J. Kugler

This is standard behavior in every language I've ever encountered.

Then you've never programmed in VB (at least 6, don't know if .net still
does this). Nested IF statements. AAAAAAAAAAAAAAAAAAAACK! Thankfully, I
program mostly in Python these days. Haven't touched VB in years. Maybe
you should have said:

"This is standard behavior in every real programming language."

There, that should start a flame war. :)

j
 
D

Dennis Lee Bieber

"This is standard behavior in every real programming language."

There, that should start a flame war. :)

Look at Ada... Which has such constructs as

if x /= 0 and then y / x < z then
...
end if


Ada also has "or else" construct. These must be used to enforce
evaluation of the LHS first, and skip evaluation of the RHS based upon
the result. With just "and" or "or", the compiler is free to reorder the
subexpressions for optimization.

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

Laurent Pointal

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

As it was replied, its standard behavior and cannot be modified.

IMHO, if you really need all your expressions to be evaluated, the clean
(most readable) way may be:

a = <first_expression which must be evaluated>
b = <second_expression(x,y,z) which must be evaluated>
if a and b :
...

A+

Laurent.
 
B

Bruno Desthuilliers

Joshua J. Kugler a écrit :
Then you've never programmed in VB (at least 6, don't know if .net still
does this). Nested IF statements. AAAAAAAAAAAAAAAAAAAACK!

I do remember an even brain-deadiest language that not only didn't
short-circuit boolean operators but also didn't have an "elif" statement...
 
S

Stef Mientki

John said:
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: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
 
L

Laurent Pointal

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

Diez B. Roggisch

Stef said:
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:
5

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

No!!! Don't think that!

Choose different numbers, and you will see that.

The semantics for the and/or operators are spelled out like this
(extra-verbose):


def and(a, b):
if bool(a) == True:
if bool(b) == True
return b # because it holds True == bool(b)
return a # because it holds Fals == bool(a)

def or(a, b):
if bool(a) == True:
return a
return b


This could be considered unfortunate because it relies on the implicit
boolean nature of certain values like "", 0, 1, {} and so forth - but
it's the way it is.

However, this has _nothing_ to do with the bitwise operations! The work
on the bits of integers, and because 5 and 4 are bitwise 0b101 and
0x100, the

5 & 4 -> 4 == 0b100

and

5 | 4 -> 5 == 0x101

That's all!

If you need non-circuiting and/or, write functions and and or:

def and_(a, b):
return a and b

def or_(a, b):
return a or b

That will ensure argument evaluation.

Diez
 
S

Stef Mientki

Laurent said:
Stef Mientki a écrit :

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.
Sorry you're completely right,
and indeed I must have something very stupid !!

thanks very much
Stef Mientki
 
B

Bruno Desthuilliers

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

This is a variant[2] of the BNF notation[1] for languages grammar.
You'll find such notation in almost any programming language.

[1] http://en.wikipedia.org/wiki/Backus-Naur_form
[2] http://docs.python.org/ref/notation.html
So now I'm left with just one question:
for bitwise operations I should use &, |, ^
yes.

for boolean operations I should use and, or, xor
yes.

but after doing some test I find strange effects:
5


Nothing strange here. You now know how boolean operators works. Bitwise
operators are something different (while still related). Represent
yourself ints as bit fields, ie (restricting ourselves to 4-bits words
for the example):

0 => 0000
1 => 0001
2 => 0010
3 => 0011
4 => 0100
5 => 0101
6 => 0110
7 => 0111
8 => 1000

(etc)

The bitwise operators works this way:

1/ the & operator compares each bit of it's operands, and for each
returns '1' if both bits are '1', else '0'. So you have:

A & B => 4 & 5 => 0100 & 0101 => 0100 => 4

0100
& 0101
----
0100

2/ the | operator compares each bit of it's operands, and for each
returns '1' if one of the bits is '1', else '0'. So you have:

A | B => 4 | 5 => 0100 | 0101 => 0101 => 5

0100
| 0101
----
0101


HTH
 
S

Steven D'Aprano

Joshua J. Kugler a écrit :

I do remember an even brain-deadiest language that not only didn't
short-circuit boolean operators but also didn't have an "elif" statement...


Is it a secret?

I'm a little perplexed at why you say a language without "elif" is a good
sign of brain-death in a programming language. I understand that, given
the parsing rules of Python, it is better to use elif than the equivalent:

if condition:
pass
else:
if another_condition:
pass


But that's specific to the syntax of the language. You could, if you
choose, design a language where elif was unnecessary:

if condition:
pass
else if another_condition:
pass

What advantage is there to "elif", apart from it needing three fewer
characters to type?
 
N

Neil Cerutti

But that's specific to the syntax of the language. You could,
if you choose, design a language where elif was unnecessary:

if condition:
pass
else if another_condition:
pass

What advantage is there to "elif", apart from it needing three
fewer characters to type?

It's a great boon to the authors of auto-indenting text editors.
 
P

Paul Boddie

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 ;-)

Delphi is based on Pascal, and from what I can recall from my
university textbook, there isn't any mandatory short-circuit
evaluation in Pascal: it's an implementation-dependent feature.
Consequently, an expression involving boolean operators in such
languages may well evaluate each term (potentially causing side-
effects) before determining the final result.

Paul
 
R

Roel Schroeven

Paul Boddie schreef:
Delphi is based on Pascal, and from what I can recall from my
university textbook, there isn't any mandatory short-circuit
evaluation in Pascal: it's an implementation-dependent feature.
Consequently, an expression involving boolean operators in such
languages may well evaluate each term (potentially causing side-
effects) before determining the final result.

I even thought Pascal never uses short-circuit evaluation, and always
evaluates all terms. I might be wrong about that though; it's been quite
a long time since I've used Pascal.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :
Is it a secret?

I'm a little perplexed at why you say a language without "elif" is a good
sign of brain-death in a programming language. I understand that, given
the parsing rules of Python, it is better to use elif than the equivalent:

if condition:
pass
else:
if another_condition:
pass


But that's specific to the syntax of the language. You could, if you
choose, design a language where elif was unnecessary:

if condition:
pass
else if another_condition:
pass

What advantage is there to "elif", apart from it needing three fewer
characters to type?

Sorry, I forgot to mention the language did not allow to have else & if
in the same statement. IOW :

if some_condition then
do_sometehing
else
if some_other_condition then
do_something_else
else
if yet_another_condition then
do_yet_another_thing
else
if your_still_here then
give_up('this language is definitively brain dead')
end if
end if
end if
end if
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top