Bool Is a Subclass of Int in Python

In Python, bool is a subclass of int: True is the integer 1 and False is 0. So isinstance(True, int) is True, and True + True == 2 (PEP 285).

  • A type check like isinstance(x, (int, float)) silently accepts True and False. To reject booleans, check for them explicitly first: isinstance(x, bool) or not isinstance(x, (int, float)). The two conditions never overlap: anything that fails the number check is not an int, so it cannot be a bool either.
  • type(x) in (int, float) also rejects booleans, because type(True) is exactly bool, but it rejects every other subclass too (NumPy numbers, for example).