Sub topic 10
bool is a subclass of int
This chapter assumes you know basic Python syntax and the built-in types int, str, and list. You should also know what truthiness is: bool(value) decides whether a value counts as true or false. Here you will see how bool relates to int, what that allows, and which bugs it causes.
The class relationship
bool is a subclass of int, so every bool is also an int. True behaves like the integer 1 and False like the integer 0. Python creates exactly two bool instances, and bool cannot be subclassed.
You can check this directly:
print(isinstance(True, int)) # True
print(issubclass(bool, int)) # True
print(bool.__mro__) # (<class 'bool'>, <class 'int'>, <class 'object'>)
print(True == 1, False == 0) # True True
print(True is 1) # False (identity differs; this may also print a SyntaxWarning)True == 1 is true because bool inherits integer equality. True is 1 is false because they are two distinct objects. Chapter material on is versus == covers this distinction in detail.
Arithmetic on booleans
Because a bool is an int, arithmetic works on it. The result of arithmetic is a plain int, not a bool.
print(True + True) # 2
print(True * 10) # 10
print(type(True + False)) # <class 'int'>
print(type(True & False)) # <class 'bool'>
print(True | False) # True
print(True ^ True) # FalseThe bitwise operators &, |, and ^ are special. When both operands are bool, bool overrides them to return a bool.
Useful idiom: counting with sum()
Summing booleans counts how many are true. This is a common and readable idiom.
scores = [55, 72, 90, 41, 68]
passed = sum(score >= 60 for score in scores)
print(passed) # 3Each comparison yields True or False, and sum() adds them as 1 and 0.
Gotchas
Bitwise not is not logical not
~True applies integer inversion to 1, so it gives -2. That value is truthy. Use not for logical negation.
print(~True) # -2
print(not True) # False
print(bool(~False)) # True, because ~False is -1Dictionary keys and set members collide
True and 1 hash the same and compare equal. A dictionary or set therefore treats them as one key.
d = {1: "one"}
d[True] = "true"
print(d) # {1: 'true'}
print({0, False, 0.0}) # {0}The original key stays and the value is overwritten. This can silently merge entries when a collection mixes flags and numbers.
Type checks accept booleans
Code that validates "an integer" with isinstance(x, int) also accepts True and False. If a boolean should be rejected, test for bool first or compare the exact type.
def is_real_int(x):
return isinstance(x, int) and not isinstance(x, bool)
print(is_real_int(5)) # True
print(is_real_int(True)) # False
print(type(True) is int) # FalseUsing type(x) is int also rejects bool, but it rejects every other int subclass too. The isinstance form with an explicit bool exclusion is more precise.
Summary of behavior
| Expression | Result | Type |
|---|---|---|
| `True + True` | `2` | `int` |
| `True & False` | `False` | `bool` |
| `True == 1` | `True` | `bool` |
| `~True` | `-2` | `int` |
| `sum([True, False, True])` | `2` | `int` |
Exercise
What does this code print, and why? d = {} d[1] = "a" d[True] = "b" d[1.0] = "c" print(d, len(d))
Show answer
It prints {1: 'c'} 1. The values 1, True, and 1.0 are equal and have the same hash, so they are the same key. The first key object (1) is kept, and each later assignment overwrites its value, so the final value is "c".
Exercise
Write a function counttrue(values) that returns how many items in a list are exactly the bool True, ignoring the integer 1. For example, counttrue([True, 1, False, True]) should return 2.
Show answer
def count_true(values): return sum(v is True for v in values) The expression v is True is false for the integer 1 even though 1 == True. Using isinstance(v, bool) and v would also work.