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 acceptsTrueandFalse. 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 anint, so it cannot be abooleither. type(x) in (int, float)also rejects booleans, becausetype(True)is exactlybool, but it rejects every other subclass too (NumPy numbers, for example).