6.4 Assignment Operations
Why all assignment in Python is reference assignment, why there's no true separate value-assignment mechanism, and how that plays out differently for mutable versus immutable objects.
Reference Assignment
b = a makes b point at the exact same object a does — no data is copied, as covered in 6.2 Objects and Variable References.
There’s No Separate “Value Assignment”
What Is It?
Python doesn’t truly have a separate “value assignment” the way some languages do — all assignment is reference assignment.
Why Does It Matter?
For an immutable value like an int, this distinction rarely matters in practice, because the value itself can’t be mutated — any operation that looks like a change actually rebinds the name to a brand-new object.
>>> c = 5
>>> d = c # d references the same int object as c
>>> d += 1 # this REBINDS d to a new object -- doesn't mutate the int
>>> c, d
(5, 6)
Effects on Mutable Objects
How Is It Used?
The consequence of reference assignment becomes visible specifically when the shared object is mutable — append() mutates the object in place, so every name referencing it sees the change:
>>> a = [1, 2]
>>> b = a
>>> b.append(3) # mutates the shared object in place
>>> a, b
([1, 2, 3], [1, 2, 3])
Rebind vs Mutate
| Operation | What Actually Happens | Visible Through Other References? |
|---|---|---|
d += 1 (int) | Rebinds d to a new int object | No — other names still point at the old value |
b.append(3) (list) | Mutates the existing object in place | Yes — every reference to it sees the change |
t = t + (3,) (tuple) | Creates a brand-new tuple, rebinds t | No — see 5.9 Mutable vs Immutable Types |
Quick Interview Answer
“Python has no separate ‘value assignment’ mechanism — every assignment binds a name to an object reference.
d = c; d += 1looks like it mutatesc’s value, but for an immutableintit actually rebindsdto a brand-new object, leavingcuntouched. The sameb = a; b.append(3)pattern on a mutablelistmutates the one shared object in place, so the change is visible through bothaandb. The difference isn’t the assignment — it’s whether the object being pointed at is mutable.”
Common Mistakes
- Assuming
+=always mutates in place — for immutable types (int,str,tuple) it rebinds; for mutable types (list, via__iadd__) it does mutate in place, which is a subtle and commonly missed distinction. - Forgetting that reassigning a variable never affects other names that shared its old object — only mutation does.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form