8

Python considers a boolean as an integer. Why?

>>> boolean = True
>>> isinstance(boolean, int)
True

Reverse the condition and ask Python if an integer is a boolean (obviously no), you get this:

>>> integer = 123
>>> isinstance(integer, bool)
False
martineau
  • 119,623
  • 25
  • 170
  • 301
Chay Huan
  • 123
  • 1
  • 10
  • 3
    You can read about the adoption of the `bool` type at [PEP 285](https://www.python.org/dev/peps/pep-0285/). That should help clear up a lot of questions you have. – Patrick Haugh Dec 05 '17 at 02:06

1 Answers1

17

It is in the documentation: Numeric Types

There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers.

True and False are numerically equal to 1 and 0 in Python, respectively.

Because bool is a subclass of int, as established above, isinstance returns true, as documented:

isinstance(object, classinfo)

Return True if the object argument is an instance of the classinfo argument, or of a subclass thereof.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
James
  • 32,991
  • 4
  • 47
  • 70