473,809 Members | 2,876 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
33 2573
On 2007-08-03, Ed Leafe <ed@leafe.comwr ote:
On Aug 3, 2007, at 11:57 AM, Bruno Desthuilliers wrote:
>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_cond ition then
do_something_el se
else
if yet_another_con dition 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

Usually that's because the language provides a switch/case
statement construct. If it does and you try to write the above
code, it isn't the language that's brain-dead! ;-)
The switch statements I'm aware of are less generally applicable
than a tower of "if { else if }* else". For example, with a
switch statement you have to dispatch on the one value for every
case.

In some languages, it's even of more limited, e.g., C, which can
switch on only integers.

--
Neil Cerutti
Next Sunday Mrs. Vinson will be soloist for the morning service. The pastor
will then speak on "It's a Terrible Experience." --Church Bulletin Blooper
Aug 3 '07 #21
John Machin wrote:
>
(you_are_confus ed and/or
function_return s_bool_but_has_ _side_effects() )
That above expression should be written more explicitly like:

function_result = function_return ing_bool_but_wi th_side_effects ()

if you_are_confuse d or function_result :
do_something_ni ce()
--irmen
Aug 3 '07 #22

"Stef Mientki" <S.************ **@mailbox.kun. nlwrote in message
news:eb******** *************** ****@news.speed linq.nl...
| John Machin wrote:
| 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
>>B and A
4

| >>A & B
| 4
| >>A or B
| 4
>>B or A
5

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

No, you hypnotised yourself by generalizing from insufficient data.
Repeat experiment with, for instance, 3 and 4 instead of 4 and 5. Then 3&4
= 0, 3|4 = 7.

tjr

Aug 3 '07 #23
Ian Clark 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 ?

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?
Pascal, and apparently Fortran, do not use short-circuit evaluation. I
remember learning this gotcha in my seventh-grade Pascal class (plus I
just googled it to make sure my memory was correct!).

Frank
Aug 4 '07 #24
On Aug 2, 10:47 pm, Stef Mientki <S.Mientki-nos...@mailbox. kun.nl>
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.
Aug 4 '07 #25
On Aug 4, 4:18 pm, Paddy <paddy3...@goog lemail.comwrote :
On Aug 2, 10:47 pm, Stef Mientki <S.Mientki-nos...@mailbox. kun.nl>
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.
And here are the bits for boolean OR:
print "\n\nShort circuit: OR"
for i in (True, False):
for j in (True, False):
print i,j,":", f(i) or g(j)

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

- Paddy.

Aug 4 '07 #26
PY help,

Using sqlite3 v3.1.3

When I create a table collumn using;

newcollum VARCHAR(35),

I get a default of 10 spaces.

No matter what I set the size to I get 10 spqces,
even using varchar(0) defaults to 10 spaces.

I would appreciae the help if someone could tell
me what I'm missing, I want to varry the column
sizes.

jim-on-linux

Aug 4 '07 #27
On Aug 4, 6:51 pm, jim-on-linux <inq1...@inqvis ta.comwrote:
PY help,

Using sqlite3 v3.1.3

When I create a table collumn using;

newcollum VARCHAR(35),

I get a default of 10 spaces.

No matter what I set the size to I get 10 spqces,
even using varchar(0) defaults to 10 spaces.

I would appreciae the help if someone could tell
me what I'm missing, I want to varry the column
sizes.

jim-on-linux
Hi Jim,
You need to create a new thread for this new question so it gets
maximum visibility and so is more likely to be answered.

- Paddy.

Aug 4 '07 #28
On Sat, 2007-08-04 at 13:51 -0400, jim-on-linux wrote:
PY help,

Using sqlite3 v3.1.3

When I create a table collumn using;

newcollum VARCHAR(35),

I get a default of 10 spaces.

No matter what I set the size to I get 10 spqces,
even using varchar(0) defaults to 10 spaces.

I would appreciae the help if someone could tell
me what I'm missing, I want to varry the column
sizes.
What you're missing is that sqlite columns are type-less. Column type
and size are irrelevant:
>>import sqlite3
conn = sqlite3.connect (":memory")
cur = conn.cursor()
cur.execute(" create table t1 (c1 varchar(35))")
<sqlite3.Curs or object at 0xb7f6dbf0>
>>cur.executema ny("insert into t1(c1) values(?)",
.... [ ("X"*i*10,) for i in range(10) ] )
>>cur.execute(" select * from t1")
<sqlite3.Curs or object at 0xb7f6dbf0>
>>for row in cur: print row
....
(u'',)
(u'XXXXXXXXXX', )
(u'XXXXXXXXXXXX XXXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXX', )
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXX', )
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXX',)

Even though the column was created to be 35 characters wide, it'll
happily accept 100-character strings.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 4 '07 #29
On Saturday 04 August 2007 14:05, Carsten Haese
wrote:
On Sat, 2007-08-04 at 13:51 -0400, jim-on-linux
wrote:
PY help,

Using sqlite3 v3.1.3

When I create a table collumn using;

newcollum VARCHAR(35),

I get a default of 10 spaces.

No matter what I set the size to I get 10
spqces, even using varchar(0) defaults to 10
spaces.

I would appreciae the help if someone could
tell me what I'm missing, I want to varry the
column sizes.

What you're missing is that sqlite columns are
type-less. Column type

and size are irrelevant:
>import sqlite3
conn = sqlite3.connect (":memory")
cur = conn.cursor()
cur.execute("c reate table t1 (c1
varchar(35)) ")

<sqlite3.Curs or object at 0xb7f6dbf0>
>cur.executeman y("insert into t1(c1)
values(?)",

... [ ("X"*i*10,) for i in range(10) ] )
>cur.execute("s elect * from t1")

<sqlite3.Curs or object at 0xb7f6dbf0>
>for row in cur: print row

...
(u'',)
(u'XXXXXXXXXX', )
(u'XXXXXXXXXXXX XXXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXX', )
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX
XXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX
XXXXXXXXXXXXXX XX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX
XXXXXXXXXXXXXX XXXXXXXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX
XXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXX',)
(u'XXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX
XXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XX',
)

Even though the column was created to be 35
characters wide, it'll happily accept
100-character strings.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

Right, the data is there.
My question is framed wrong. Your answer pointed
out the flaw in the question.

Since I'm on linux, the database can be opened
with Knoda. Once opened the table collumns are
always 10 spaces so all the data is not readable
as presented. Not quite a python problem.

I can Tk a display for the data, just thought I
could save time by letting the user open the
table and read the data right from the table.

Thanks for the help.

jim-on-linux








Aug 4 '07 #30

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
43430
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
2122
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
7362
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
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
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...
0
5548
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
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
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.