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!
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 this is not guaranteed as it is possible for
TrueandFalseto be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.In Python 3.x
TrueandFalseare keywords and will always be equal to1and0.Under normal circumstances in Python 2, and always in Python 3:
Falseobject is of typeboolwhich 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 and the docs for Python 3 both say:
and in the boolean subsection:
There is also, for Python 2:
So booleans are explicitly considered as integers in Python 2 and 3.
So you’re safe until Python 4 comes along. 😉