1. Names vs. Boxes: The Python Object Model
Names Bound to Heap Entities
- In languages like C, int x = 5 creates an 8-byte box labeled x where 5 resides.
- In Python, x = 5 creates an integer object 5 in heap memory and binds the identifier "x" to point to it.
- When you do y = x, Python does NOT copy the object 5. It simply binds a second identifier "y" to the identical object in memory.
- You can inspect object identity in RAM using the built-in id() function.
x = [1, 2, 3]
y = x # y is an alias referencing the exact same list
print("id(x) == id(y):", id(x) == id(y))
y.append(4)
print("x after y.append:", x) # x is mutated!