Memory Management & Identity
Understand object references, mutable default argument traps, and identity (is) vs equality (==).
Learning objectives
- Differentiate object equality (==) from object identity (is)
- Avoid the mutable default argument trap in function signatures
Lesson material
Identity vs Equality
== compares values, whereas is compares memory address location (id(a) == id(b)).
Example code
a = [1, 2, 3]
b = [1, 2, 3]
print("a == b:", a == b)
print("a is b:", a is b)
Practice exercise: Fix Mutable Default Argument Trap
Fix the function `add_item(item, target=[])` so that passing no target creates a fresh list every time rather than sharing a mutable default.
Test yourself with the Module 21: Python Internals & Execution Model quiz →