473,799 Members | 3,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(no) fast boolean evaluation ?

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
Aug 2 '07 #1
33 2570
En Thu, 02 Aug 2007 18:47:49 -0300, Stef Mientki
<S.************ **@mailbox.kun. nlescribió:
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.

--
Gabriel Genellina

Aug 2 '07 #2
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?

Ian

Aug 2 '07 #3
On 8/2/07, Stef Mientki <S.************ **@mailbox.kun. nlwrote:
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.

--
Evan Klitzke <ev**@yelp.co m>
Aug 2 '07 #4
On Aug 3, 8:55 am, Ian Clark <icl...@mail.ew u.eduwrote:
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_confus ed and/or
function_return s_bool_but_has_ _side_effects() )

Aug 2 '07 #5
On Aug 3, 9:19 am, "Evan Klitzke" <e...@yelp.comw rote:
On 8/2/07, Stef Mientki <S.Mientki-nos...@mailbox. kun.nlwrote:
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.
If each operand is of type bool (or, more generally,
isinstance(oper and, int) is true), you could trivially use the & and |
operators.

Aug 2 '07 #6
On Thursday 02 August 2007 15:19, Evan Klitzke wrote:
>I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).

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. AAAAAAAAAAAAAAA AAAAACK! 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

--
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/ Â*ID 0xDB26D7CE

Aug 3 '07 #7
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_expressi on which must be evaluated>
b = <second_express ion(x,y,z) which must be evaluated>
if a and b :
...

A+

Laurent.
Aug 3 '07 #8
Joshua J. Kugler a écrit :
On Thursday 02 August 2007 15:19, Evan Klitzke wrote:
>>I discovered that boolean evaluation in Python is done "fast"
(as soon as the condition is ok, the rest of the expression is ignored).
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. AAAAAAAAAAAAAAA AAAAACK!
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...

Aug 3 '07 #9
John Machin wrote:
On Aug 3, 8:55 am, Ian Clark <icl...@mail.ew u.eduwrote:
>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_confus ed and/or
function_return s_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
Aug 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
8
43968
by: FrancisC | last post by:
how to define Boolean in C ? boolean abc; ??? is it default as True or False??
5
43428
by: Dave | last post by:
Hello all Is there a yes/no boolean datatype with mySQL? I can't seem to find if there is, and I have used an int type set to 1 or 0 but that breaks some of my apps that used to use access which does have a yes/no field. Many thanks Dave
43
2121
by: Shehab Kamal | last post by:
Which approach is better: 1) Use a method that returns true/false (the method will log the exception that lead to false using log4net for example) OR 2) Use a method that returns void and throws an exception in case of failure? If the second approach is to be suggested: Should .NET predefined exceptions, NullReferenceException, IndexOutOfRangeException and so on, or my own exceptions be thrown?
7
7358
by: rguarnieri | last post by:
Hi! I'm trying to create a query with a boolean expression like this: select (4 and 1) as Value from Table1 this query return always -1, but when I make the same calculation in visual basic, the value returned is 0. Can anyone tell me why the expression (4 and 1) return different value
3
1266
by: =?Utf-8?B?RW1tYQ==?= | last post by:
Hello I have a database which I access through the Internet using ASP now my SELECT statement isn't working now that I've changed the text values of Yes/No to boolean values of Yes/No. What can I do to resolve this problem?
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10482
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.