473,698 Members | 2,557 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
18 2875

"Daniel Klein" <da*****@aracne t.com> wrote in message
news:vr******** *************** *********@4ax.c om...
On Wed, 06 Aug 2003 12:18:45 -0400, Peter Hansen <pe***@engcorp. com>
wrote:
def boolean2String( boolVal):
return { False : '1', True : '0' } [ not boolVal ]
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]


Your function requires the arg to be 0/1 (True/False). Peter's
function works for *any* input that can be 'not'ed.

TJR
Jul 18 '05 #11
Peter Hansen <pe***@engcorp. com> writes:
As Skip said... but it should have had a comment anyway, since it
wasn't clear. You could also consider using "not not boolVal" if
you want to make the code _slightly_ (IMHO) more readable, and slightly
slower, but I think one still needs a comment explaining it. :-(


I find

def boolean2str(val ):
if val:
return '1'
else:
return '0'

both more readable, and twice as fast. It doesn't need to create a
dictionary each time, and it does not to perform a dictionary lookup.
If you prefer compactness, use

def boolean2str(val , results=('1', '0')):
return results[not val]

Regards,
Martin
Jul 18 '05 #12

"Martin v. Löwis" <ma****@v.loewi s.de> wrote in message
news:m3******** ****@mira.infor matik.hu-berlin.de...
If you prefer compactness, use

def boolean2str(val , results=('1', '0')):
return results[not val]


-or, more safely (no default arg to accidently overwrite)-

def boolean2str(val ):
return val and '1' or '0'

But one might want the flexibility of intentionally replacing the
results default, as in

print boolean2str(som evalue, ('T', 'F'))

Terry J. Reedy


Jul 18 '05 #13

"John Roth" <ne********@jhr othjr.com> wrote in message
news:vi******** ****@news.super news.com...

"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


Then, what is the best way to write boolean operations for Python 2.1 so
that it will be as 2.3+ ready as possible?

Should we just use 0 and 1?

Until the vendor of the tool we are using delivers a more recent version of
Python with the product, we must produce 2.1 compatible code.

Thanks.
Jul 18 '05 #14
In article <3f**********@t hemost.net>,
"Paul Watson" <pw*****@redlin ec.com> wrote:
Then, what is the best way to write boolean operations for Python 2.1 so
that it will be as 2.3+ ready as possible?


I've been including the following at the start of some of my code:

if 'True' not in globals():
globals()['True'] = not None
globals()['False'] = not True

My hope is that setting up True and False in this convoluted way will
allow it to continue to work in some future version where assignment to
builtins is disallowed.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #15
On Sun, Oct 19, 2003 at 08:17:15AM -0700, David Eppstein wrote:
In article <3f**********@t hemost.net>,
"Paul Watson" <pw*****@redlin ec.com> wrote:
Then, what is the best way to write boolean operations for Python 2.1 so
that it will be as 2.3+ ready as possible?


I've been including the following at the start of some of my code:

if 'True' not in globals():
globals()['True'] = not None
globals()['False'] = not True


Why not simply:

try:
True
except NameError:
True = (1 == 1) # or not None, if you prefer
False = not True

Or were you trying to change the __builtins__ by using globals()?

-Andrew.
Jul 18 '05 #16
David Eppstein <ep******@ics.u ci.edu> wrote in news:eppstein-
D2************* ******@news.ser vice.uci.edu:
Then, what is the best way to write boolean operations for Python 2.1 so
that it will be as 2.3+ ready as possible?


I've been including the following at the start of some of my code:

if 'True' not in globals():
globals()['True'] = not None
globals()['False'] = not True

My hope is that setting up True and False in this convoluted way will
allow it to continue to work in some future version where assignment to
builtins is disallowed.


Since True will never be in globals unless you assign it there, you might
as well just drop the if statement altogether. Also I fail to see what
benefit you gain from the contorted assignment into the globals dictionary.
Why not just write:

True = not None
False = not True

It has the same effect overall.

If you want to avoid hiding the builtin True and False, then use try..catch
to detect them.

--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #17
Andrew Bennetts wrote:
On Sun, Oct 19, 2003 at 08:17:15AM -0700, David Eppstein wrote:
In article <3f**********@t hemost.net>,
"Paul Watson" <pw*****@redlin ec.com> wrote:
> Then, what is the best way to write boolean operations for Python 2.1
> so that it will be as 2.3+ ready as possible?


I've been including the following at the start of some of my code:

if 'True' not in globals():
globals()['True'] = not None
globals()['False'] = not True


Why not simply:

try:
True
except NameError:
True = (1 == 1) # or not None, if you prefer
False = not True

Or were you trying to change the __builtins__ by using globals()?

-Andrew.


I suppose David tries to avoid a syntax error raised by the assignment

True = somethingElse

in a future version of Python. Therefore he has "hidden" the builtins to be
assigned from the compiler by turning them into strings.

Peter

Jul 18 '05 #18
In article <bn************ *@news.t-online.com>,
Peter Otten <__*******@web. de> wrote:
I suppose David tries to avoid a syntax error raised by the assignment

True = somethingElse

in a future version of Python. Therefore he has "hidden" the builtins to be
assigned from the compiler by turning them into strings.


Yes, exactly.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #19

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

Similar topics

46
4234
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
2570
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, while "9 is True" is false even though 9 evaluates to True.) Anyhow, in doing my tests, I...
35
3385
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
42421
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
2469
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
30125
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
3146
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 comparisons to = 1 instead of = true? response.write True = 1 'prints False response.write True = 0 ...
90
3422
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
7394
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 ADB1541.autostart:DB2AUTOSTART TRUE I
40
2717
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
8683
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
8610
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
9170
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...
1
8902
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
7740
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
5862
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();...
1
3052
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
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.