473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

True

In Python 2.2 I use to have

true = (1 == 1)
false = not true

This was at the recommendation of someone on this list some time ago.
The reason (if I remember correctly) was that setting

true = 1
false = 0

were not true booleans.

Now the expression (1 == 1) returns 'True', and caused a bug in my
code. So my question is what is the proper method for setting booleans
in 2.3?

Really confused,

Daniel Klein
Jul 18 '05 #1
18 2863
In article <fu************ *************** *****@4ax.com>, Daniel Klein wrote:
In Python 2.2 I use to have

true = (1 == 1)
false = not true

This was at the recommendation of someone on this list some time ago.
The reason (if I remember correctly) was that setting

true = 1
false = 0

were not true booleans.

Now the expression (1 == 1) returns 'True', and caused a bug in my
Actually, it returns True, not 'True'
code. So my question is what is the proper method for setting booleans
in 2.3?

Really confused,

True and False are now built in to 2.3
python Python 2.3 (#1, Aug 1 2003, 15:18:54)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright" , "credits" or "license" for more information.
dir(__builtins_ _) ['ArithmeticErro r', 'AssertionError ', 'AttributeError ', 'DeprecationWar ning',
'EOFError', 'Ellipsis', 'EnvironmentErr or', 'Exception',

'False', .snip. 'True', .snip.

'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']

but I think if you had an up-to-date 2.2 they should have been there too...

python2.2

Python 2.2.3 (#1, Jun 9 2003, 18:01:50)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright" , "credits" or "license" for more information. dir(__builtins_ _)

['ArithmeticErro r', 'AssertionError ', 'AttributeError ', 'DeprecationWar ning',
'EOFError', 'Ellipsis', 'EnvironmentErr or', 'Exception',

'False', .snip. 'True', .snip.

'super', 'tuple', 'type', 'unichr', 'unicode', 'vars', 'xrange', 'zip']


What error are you getting? How are you using true and false?
I know the developers were very careful to not cause any compatibilty
problems with the addition of booleans.

Jul 18 '05 #2

"Daniel Klein" <da*****@aracne t.com> wrote in message
news:fu******** *************** *********@4ax.c om...
In Python 2.2 I use to have

true = (1 == 1)
false = not true

This was at the recommendation of someone on this list some time ago.
The reason (if I remember correctly) was that setting

true = 1
false = 0

were not true booleans.
There were no true booleans in 2.2 and earlier. Whoever recommended
that didn't know what he was talking about. There was no difference.
Now the expression (1 == 1) returns 'True', and caused a bug in my
code. So my question is what is the proper method for setting booleans
in 2.3?
I presume what broke your code was depending on the return from
either str() or repr(), or the % operator. That was, unfortunately, one
of the incompatibilite s between 2.2 and 2.3.

In 2.3, Boolean is a subtype of Int, and has two values: True and False.
Both of these are built in constants, so just use them. That's what they're
for. For most purposes, the are the same as 1 and 0, except for what they
return from str() and repr(), and how they get formatted with %.

John Roth

Really confused,

Daniel Klein

Jul 18 '05 #3

"Andrew Dalke" <ad****@mindspr ing.com> wrote in message
news:bg******** **@slb0.atl.min dspring.net...
Daniel Klein:
true = (1 == 1)
false = not true

This was at the recommendation of someone on this list some time ago.

John Roth
There were no true booleans in 2.2 and earlier. Whoever recommended
that didn't know what he was talking about. There was no difference.


Indeed, there were no true booleans in Python, but there were
actual PyTrue and PyFalse objects at the C level. Doing the "1 == 1"
trick would expose that implementation choice to Python.

This was rarely used. The only place I know of is in the win32 code,
where a COM API can take a boolean or an integer function. The
win32 interface checks if the value is PyTrue/PyFalse,

Here's relevant links
http://mail.python.org/pipermail/pyt...er/015260.html
http://mailman.pythonpros.com/piperm...q1/000106.html


So, are you saying that this would have returned -1, rather than 1?

John Roth
Andrew
da***@dalkescie ntific.com

Jul 18 '05 #4
[Daniel Klein]
So my question is what is the proper method for setting booleans in 2.3?


Hi, Daniel.

In 2.3, you do not need to set booleans, `True' and `False' are just there.

However, if you have a need to write portably against many versions, you
might try something like:

try:
True
except NameError:
False, True = range(2)

in modules where you need to use `True' or `False'.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #5
John Roth:
So, are you saying that this would have returned -1, rather than 1?
No, and I don't know how you thought I suggested it. If PyTrue returned
-1 before then "True = (1==1)" would not provide the right compatibility
between different Python versions and so would not be an appropriate
suggestion at all.

You must have misread the text from the second URL
http://mailman.pythonpros.com/piperm...q1/000106.html
in it, Mark Hammond said: The only thing that I can see changing is the literal value of the
boolean field - currently, the conversion from VT_BOOL to integer will
result in 0 or -1. PyTrue is 1


The boolean returned from COM is -1. PyTrue is (and was) 1.

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #6
On Mon, 04 Aug 2003 02:13:57 GMT, Lee Harr <mi*****@fronti ernet.net>
wrote:
In article <fu************ *************** *****@4ax.com>, Daniel Klein wrote:
In Python 2.2 I use to have

true = (1 == 1)
false = not true

This was at the recommendation of someone on this list some time ago.
The reason (if I remember correctly) was that setting

true = 1
false = 0

were not true booleans.

Now the expression (1 == 1) returns 'True', and caused a bug in my


Actually, it returns True, not 'True'


In this case, it _did_ return a literal string of 'True' cuz I was
using str(true) and str(false). That was the nature of the buglet.
Before str(true) would return return '1' and str(false) would return
'0'. Sorry that I didn't include this bit of information in the
original post :-(

Dan
Jul 18 '05 #7
On 04 Aug 2003 06:17:37 -0400, pi****@iro.umon treal.ca (François
Pinard) wrote:
[Daniel Klein]
So my question is what is the proper method for setting booleans in 2.3?


Hi, Daniel.

In 2.3, you do not need to set booleans, `True' and `False' are just there.

However, if you have a need to write portably against many versions, you
might try something like:

try:
True
except NameError:
False, True = range(2)

in modules where you need to use `True' or `False'.


Thanks François. I don't need to support multiple versions. I simply
changed the code from:

true = (1 == 1)

to

true = 1

and presto, no more bug :-)

Dan

Jul 18 '05 #8
On Wed, 06 Aug 2003 09:18:13 +1000, Mark Hammond
<mh******@skipp inet.com.au> wrote:
Daniel Klein wrote:
Thanks François. I don't need to support multiple versions. I simply
changed the code from:

true = (1 == 1)

to

true = 1

and presto, no more bug :-)


For the sake of being pedantic, I believe your code still does have a
bug. You bug is that you rely on the "str()" of a boolean value
returning a specific string. Your code should probably check the bool
and create your own string rather than relying on this behaviour
remaining unchanged forever.


I'm not really taking a str() on a boolean value anymore; it's being
taken on an integer value. Are you saying that...

true = 1
str(true) # this should always return '1' now and forever more

....could cause problems in the future?

The reason I'm taking a str() value is cuz it is being sent over a
socket to a non-python server process, where it is converted back to a
boolean.

Make sense now?

Dan
Jul 18 '05 #9
On Wed, 06 Aug 2003 12:18:45 -0400, Peter Hansen <pe***@engcorp. com>
wrote:
The reason I'm taking a str() value is cuz it is being sent over a
socket to a non-python server process, where it is converted back to a
boolean.


In that case, you'd be better off converting using your own routine
rather than relying on a particular behaviour of str() for this test.
Basically, do something like

def boolean2String( boolVal):
return { False : '1', True : '0' } [ not boolVal ]

and save yourself any further future pain...


Thanks Peter, that will do the trick. Just wondering though why you
chose to code the opposite values and not as

def boolean2String( boolVal):
return {True:'1', False:'0'}[boolVal]

Dan
Jul 18 '05 #10

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

Similar topics

46
4189
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
3
2556
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if True:" both lead to the conditional being executed, but True == 9 -> False, that this would be true was not obvious to me -- "True is True" is True,...
35
3353
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
36
42383
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at the end so the loop is executed at least once. It's some way the opposite of "while". So far, all I got is:
14
2449
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a "counted" string and data in a binary file. Given... #include <stdio.h> int main(int argc, char *argv) { char bstring;
48
30071
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
30
3112
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's not. Actually, True should be equal to anything but False, null, and 0. Is there a workaround for this? Or do I need to change all my...
90
3387
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
2
7381
by: Ultrak The DBA | last post by:
Using the following query: select substr(reg_var_name,1,24) as reg_var_name, substr(reg_var_value, 1,12) as reg_var_value, level from table(sysproc.reg_list_variables()) as registryinfo; I am receiving this information from hitting several of our prod databases: ADB1291.autostart:DB2AUTOSTART TRUE I...
40
2699
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) on win32 Type "help", "copyright", "credits" or "license" for more
0
7612
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...
0
8122
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...
1
7673
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...
0
7970
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...
0
6284
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...
0
5219
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...
0
3653
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...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
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...

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.