Skip to content

Bool as Subclass

In Python, bool is a subclass of int, giving boolean values numeric properties.


Numeric Behavior

Boolean values exhibit numeric behavior and participate in arithmetic operations.

1. Subclass of int

print(True + 1)  # Output: 2
print(False * 5)  # Output: 0
print(isinstance(True, int))  # Output: True

2. Internal Values

True and False are internally represented as 1 and 0:

print(True == 1)   # True
print(False == 0)  # True

3. Arithmetic Use

Boolean values can be used in arithmetic computations:

x = True
y = False
print(x + y)  # Output: 1 (since True is 1 and False is 0)

Type Coercion

Since bool is a subclass of int, it follows standard type coercion rules.

1. Implicit Conversion

print(3 * True)   # Output: 3
print(10 - False) # Output: 10

2. Coercion Rules

Boolean values seamlessly integrate with other numeric types without explicit conversion.


Type Checking

Explicit type checking can prevent unintended behavior.

1. isinstance Check

value = True
if isinstance(value, bool):
    print("This is a boolean value.")

2. Strict Checking

Use isinstance when strict type differentiation is required.


Design Implications

The bool-int relationship has important consequences.

1. Performance

Allows efficient boolean computations in numerical algorithms.

2. Integration

Facilitates seamless integration into arithmetic expressions and logical computations.

3. Caution Needed

This feature necessitates caution in contexts where strict type differentiation is required.


Conclusion

While bool is distinct semantically (representing truth, not numbers), its inheritance from int enables powerful numeric operations and optimizations in Python code.