pythonbooleanequalitylanguage-specifications

Is False == 0 and True == 1 an implementation detail or is it guaranteed by the language?


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!


Solution

  • In Python 2.x this is not guaranteed as it is possible for True and False to be reassigned. However, even if this happens, boolean True and boolean False are still properly returned for comparisons.

    In Python 3.x True and False are keywords and will always be equal to 1 and 0.

    Under normal circumstances in Python 2, and always in Python 3:

    False object is of type bool which is a subclass of int:

        object
           |
         int
           |
         bool
    

    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:

    There are two types of integers: [...] Integers (int) [...] Booleans (bool)

    and in the boolean subsection:

    Booleans: These represent the truth values False and True [...] Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

    There is also, for Python 2:

    In numeric contexts (for example when used as the argument to an arithmetic operator), they [False and True] behave like the integers 0 and 1, respectively.

    So booleans are explicitly considered as integers in Python 2 and 3.

    So you're safe until Python 4 comes along. ;-)