Mutability vs.
Reassignment
Master the crucial difference between changing an object's contents and pointing a name at a new object in Python.
Every Value is an Object
In Python, every value you create lives somewhere in memory as an object. Each object has three properties:
id()int, list, str42, [1, 2], "hi"Names Point to Objects
Mutable vs. Immutable Types
Mutability is whether an object's value can be changed in place after creation: the object stays at the same memory address, but its contents change.
list, dict, set, custom objectsint, float, str, tuple, boolWhat is Reassignment?
Reassignment makes a name point to a different object. The original object is untouched: only the name tag moves.
The Core Rule
id()).id()). The old object is unaffected.Mutation vs. Reassignment
Why This Matters: Shared References
When two names point to the same mutable object, mutating through one name is visible through the other. Reassignment only affects the reassigned name.
Reassignment Breaks the Link
Immutable Objects and "Change"
When you "change" an immutable value like an int or str, Python is actually creating a new object and reassigning the name:
The += Trap: It Depends on Type
The += operator behaves differently for mutable and immutable types:
a += [4] calls a.extend([4]): mutates in place. Same id().t += (4,) creates a new tuple and reassigns. Different id().How to Determine Mutation vs. Reassignment
list, dict, set) or immutable (int, str, tuple)?= to bind a name? That is reassignment. Does it call a method like .append(), .update(), or use index assignment like a[0] = 5? That is mutation.id(). Print id(x) before and after. Same id = mutation. Different id = reassignment.Example 1: List Mutation
Example 2: String Reassignment
Example 3: Dict Mutation in a Function
Common Mistakes
x += 1 with an int is reassignment, not mutation. Only mutable types mutate with +=.b = a expecting a copy. For mutable objects, both names share the same object. Use a.copy() or list(a).Summary Table
| Mutation | Reassignment | |
|---|---|---|
| What changes? | The object's contents | Which object the name refers to |
id() after? |
Same | Different |
| Affects aliases? | Yes: all names see the change | No: only the reassigned name |
| Works on immutables? | No | Yes: always |
| Example operations | .append(), [i]=, .update() |
x = ..., x = x + 1 |
Question 1 of 4
What does the following code print?a = [1, 2]; b = a; a.append(3); print(b)
.append() is mutation (not reassignment), both names see the change. id(a) == id(b) remains True throughout.Question 2 of 4
After running x = 10; x = x + 5, which statement is true about x?
x + 5 creates a new int object (15), and x = reassigns the name to this new object. The original object 10 is unaffected.Question 3 of 4
Which of the following is an example of mutation, not reassignment?
scores[0] = 95 uses index assignment to change an element of an existing list in place: this is mutation. The other three options all use = to bind a name to a new or different object, which is reassignment.Question 4 of 4
What is the value of original after this code?original = [1, 2]; copy = original; copy = [3, 4]
copy = [3, 4] is reassignment: it makes copy point to a brand-new list. It does not modify the list that original points to. So original is still [1, 2].