Is it guaranteed that False == 0
and True == 1
, in Python (assuming that they are not reassigned by the user)? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python (both existing and, likely, future ones)?
0 == False # True
1 == True # True
['zero', 'one'][False] # is 'zero'
Any reference to the official documentation would be much appreciated!
Edit: As noted in many answers, bool
inherits from int
. The question can therefore be recast as: "Does the documentation officially say that programmers can rely on booleans inheriting from integers, with the values 0
and 1
?". This question is relevant for writing robust code that won't fail because of implementation details!
In Python 2.x, it is not guaranteed at all:
So it could change. In Python 3.x, True, False, and None are reserved words, so the above code would not work.
In general, with booleans you should assume that while False will always have an integer value of 0 (so long as you don't change it, as above), True could have any other value. I wouldn't necessarily rely on any guarantee that
True==1
, but on Python 3.x, this will always be the case, no matter what.Very simple. As bool relates to evaluating an integer as a bool, ONLY zero gives a false answer. ALL Non-Zero values, floats, integers, including negative numbers, or what have you, will return true.
A nice example of why this is useful is determining the power status of a device. On is any non-zero value, off is zero. Electronically speaking this makes sense.
To determine true or false relatively between values, you must have something to compare it to. This applies to strings and number values, using
==
or!=
or<
,>
>=
,<=
, etc.You can assign an integer to a variable and then get true or false based on that variable value.
Just write
int(False)
and you will get0
, if you typeint(True)
it will output1
Link to the PEP discussing the new bool type in Python 2.3: http://www.python.org/dev/peps/pep-0285/.
When converting a bool to an int, the integer value is always 0 or 1, but when converting an int to a bool, the boolean value is True for all integers except 0.
False is a bool. It has a different type. It is a different object from 0 which is an integer.
0 == False
returns True because False is cast to an integer. int(False) returns 0The python documentation of the == operator says (help('==')):
As a consequence False is converted to an integer for the need of the comparison. But it is different from 0.
In Python 2.x this is not guaranteed as it is possible for
True
andFalse
to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.In Python 3.x
True
andFalse
are keywords and will always be equal to1
and0
.Under normal circumstances in Python 2, and always in Python 3:
False
object is of typebool
which is a subclass ofint
:It is the only reason why in your example,
['zero', 'one'][False]
does work. It would not work with an object which is not a subclass of integer, because list indexing only works with integers, or objects that define a__index__
method (thanks mark-dickinson).Edit:
It is true of the current python version, and of that of Python 3. The docs for python 2.6 and the docs for Python 3 both say:
and in the boolean subsection:
So booleans are explicitly considered as integers in Python 2.6 and 3.
So you're safe until Python 4 comes along. ;-)