472,793 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,793 software developers and data experts.

Silly questions about True and False

drs
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 accidentally
typed
False = 0
rather than
False == 0
and I lost the False statement.
Thus,
False 0

To get it back, I found that I could do
False = (1 == 2)
which seems to put False back to False, but this seems weird.
1 = 0
throws an error (can't assign to literal), why doesn't False = 0 throw the
same error? Also, why doesn't False = 0 make
1 == 2

0

Instead of False?

-d
Jul 18 '05 #1
3 2466
drs wrote:
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.)
What do you mean by "9 evalutes to True"? That's not the
case. bool(9) does evaluate to True, and that's effectively
what "if 9:" is doing...

Anyhow, in doing my tests, I accidentally typed

False = 0 rather than
False == 0
and I lost the False statement.
To get it back, you should do "del False". Remarkably,
this actually just removes the local name False that
you created which was "shadowing" the builtin name,
allowing the builtin name (which you didn't change)
to be seen again.

Note that this name you are creating is actually *local*
to the module you are in, which at the interactive
prompt is called __main__. Thus you are not changing
False from the point of view of any other module, or
of the Python internals. They are (for the most part)
still getting it from the builtin module.

which seems to put False back to False, but this seems weird.1 = 0
throws an error (can't assign to literal), why doesn't False = 0 throw the
same error?
False is not a constant, it's merely a name. 1 and 0 are
constants. You can't change a constant, but you *can*
"rebind" a name (that is, attach it to something else).
That's all you're doing here.
Also, why doesn't False = 0 make1 == 2

0
Instead of False?


Because such comparisons are all effectively doing a bool()
which continues to return the builtin False.

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
drs wrote:
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.)

What do you mean by "9 evalutes to True"? That's not the
case. bool(9) does evaluate to True, and that's effectively
what "if 9:" is doing...

Anyhow, in doing my tests, I accidentally
typed

> False = 0


rather than
> False == 0

and I lost the False statement.

I should point out a terminological inexactitude here: False is not a
statement, it's an identifier. As Peter pointed out, that identifier can
exist in several different namespaces, leading to your initial confusion.
To get it back, you should do "del False". Remarkably,
this actually just removes the local name False that
you created which was "shadowing" the builtin name,
allowing the builtin name (which you didn't change)
to be seen again.

Note that this name you are creating is actually *local*
to the module you are in, which at the interactive
prompt is called __main__. Thus you are not changing
False from the point of view of any other module, or
of the Python internals. They are (for the most part)
still getting it from the builtin module.

which seems to put False back to False, but this seems weird.
> 1 = 0

throws an error (can't assign to literal), why doesn't False = 0 throw
the
same error?

False is not a constant, it's merely a name. 1 and 0 are
constants. You can't change a constant, but you *can*
"rebind" a name (that is, attach it to something else).
That's all you're doing here.

Note, however, that the 2.4 documentation does actually list True and
False as constants (along with None) in section 2.5 of the Library
Reference manual.
Also, why doesn't False = 0 make
> 1 == 2


0
Instead of False?

Because such comparisons are all effectively doing a bool()
which continues to return the builtin False.


Sadly, the builtin False and True can actually be overwritten (unlike
None, which starting from 2.4 really *is* a constant):

$ python
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
None = 42 SyntaxError: assignment to None True = 33
True 33 __builtins__.True True __builtins__.True = 42
True 33 del True
True 42 Of course, even though you can change True's value, you can't make a
comparison return __builtins__.True:
None is None True (None is None) + 1 2


Here the comparison is returning the Python object to which
__builtins__.True initially refers.

when-i-tell-you-three-times-it-is-forty-true-ly y'rs - steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #3
drs wrote:
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
Note that evaluating to True isn't the same as being equal to True.
Would you also expect that [] == None? Both evaluate to False in a
boolean context. Or an even better example, would you expect that
[1] == [2]? Both of these evaluate to True in a boolean context...

What you should expect is that:

py> bool([]) == bool(None) == False
True
py> bool([1]) == bool([2]) == True
True
"True is True" is True, while "9 is True" is false
even though 9 evaluates to True.)
Be careful here. 'is' tests object identity. So "9 is True" would only
evaluate to True if 9 is exactly the same object as True. 'is' won't
convert anything to a boolean; "x is y" basically translates to
"id(x) == id(y)".
why doesn't False = 0 throw the same error?
Backwards compatibility. A lot of modules pre-False and True have code
that looks like:

True, False = 1, 0

or

True, False = (1 == 1), (1 == 0)

If assignment to True or False became a syntax error (which it probably
should be), it would break all such code.
Also, why doesn't False = 0 make
1 == 2

0

Instead of False?


You've only rebound the name False in your module's namespace. 1 == 2
executes the int code for equality, which presumably returns one of
Py_False or Py_True, not your binding for the name False in your module.

Steve
Jul 18 '05 #4

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

Similar topics

0
by: Ray Tayek | last post by:
hi, trying to convert some csv files into xsml and pulling a few hairs out :(. using the files below. a java program will parse the csv and take care of strange names and notes that line breaks in...
0
by: Henry | last post by:
Using ideas provided by some of you I was able to figure out how to get the names of the parameters fields of a crystal report specified at run time. The code below just basically puts the...
1
by: TBK | last post by:
I'm trying to finish up an assignment I had for a class and basically I'm stuck. The program is supposed to take information you've entered, put it into an array, display it in a listbox and then...
2
by: Mark P | last post by:
Consider the following snippet of code to read lines from a text file: ifstream file_stream( "some_file.txt"); string read_line; while( file_stream) { getline( file_stream, read_line); } ...
5
by: rhino | last post by:
I was asking questions about the DB2 certification exams a couple of weeks ago and I realize I forgot to ask two questions. I suspect I know the answers but it would be very helpful to get...
23
by: sophia.agnes | last post by:
Dear all, I was going through a C book written by an indian author the following are the questions given in that book 1) consider the following statement: s1 : an operator may require an...
8
by: Geoff Cox | last post by:
Hello, I cannot see what is wrong with this. Even though the loop gives a value for each of the 36 values of the lab_result array I get the "Please answer all the questions" alert. Why?! ...
9
by: Euvin | last post by:
Const Pi as Double Would you consider Double to be a Variable? And what does Double mean? What is its purpose?
6
by: IReallyNeedHelp | last post by:
I have saved the questions using AddQuestion.aspx page i have created but i don't know how to display it and calculate their score. this is the formview i have done, but there is some error ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.