01 / 21

Mutability vs.
Reassignment

Master the crucial difference between changing an object's contents and pointing a name at a new object in Python.

Python

Every Value is an Object

In Python, every value you create lives somewhere in memory as an object. Each object has three properties:

Identity
Its unique memory address, checked with id()
Type
What kind of object it is: int, list, str
Value
The actual data it holds: 42, [1, 2], "hi"

Names Point to Objects

x y int object 42 list object [1, 2, 3] NAMES OBJECTS IN MEMORY refers to refers to

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.

Mutable
Can be changed in place.
list, dict, set, custom objects
Immutable
Cannot be changed in place.
int, float, str, tuple, bool

What is Reassignment?

Reassignment makes a name point to a different object. The original object is untouched: only the name tag moves.

BEFORE: x = 5 x 5 id: 0xA1 AFTER: x = 10 x 5 id: 0xA1 10 id: 0xB2 new object!

The Core Rule

1
Mutation changes the object's contents. The name still points to the same object (same id()).
2
Reassignment makes the name point to a different object (new id()). The old object is unaffected.
3
Only mutable objects support mutation. Immutable objects can only ever be "replaced" via reassignment.

Mutation vs. Reassignment

MUTATION a.append(4) a [1, 2, 3] id: 0xC3 same id [1, 2, 3, 4] id: 0xC3 Same object, new contents REASSIGNMENT b = b + [4] b [1, 2, 3] id: 0xD4 [1, 2, 3, 4] id: 0xE5 New object, name re-bound Mutation: same id, changed data | Reassignment: different id

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.

a = [1, 2, 3] b = a # b points to the SAME list a.append(4) # mutation: changes the shared object print(b) # [1, 2, 3, 4] ← b sees the change!

Reassignment Breaks the Link

a = [1, 2, 3] b = a a = [10, 20] # reassignment: a now points to a NEW list print(b) # [1, 2, 3] ← b is unaffected print(a) # [10, 20]
b [1, 2, 3] a [10, 20] original object new object

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:

x = 5 print(id(x)) # e.g., 140234567 x = x + 1 # creates NEW int object 6, reassigns x print(id(x)) # e.g., 140234599 ← different!

The += Trap: It Depends on Type

The += operator behaves differently for mutable and immutable types:

Mutable (list)
a += [4] calls a.extend([4]): mutates in place. Same id().
Immutable (tuple)
t += (4,) creates a new tuple and reassigns. Different id().
lst = [1, 2]; print(id(lst)) # 0xA1 lst += [3]; print(id(lst)) # 0xA1 ← same! tup = (1, 2); print(id(tup)) # 0xB2 tup += (3,); print(id(tup)) # 0xC3 ← different!

How to Determine Mutation vs. Reassignment

1
Identify the type. Is the object mutable (list, dict, set) or immutable (int, str, tuple)?
2
Look at the operation. Does it use = 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.
3
Check with id(). Print id(x) before and after. Same id = mutation. Different id = reassignment.
4
Consider aliases. If other names point to the same object, mutation affects them all. Reassignment does not.

Example 1: List Mutation

colors = ["red", "blue"] palette = colors # alias: same object colors[1] = "green" # index assignment = mutation print(colors) # ["red", "green"] print(palette) # ["red", "green"] ← also changed! print(colors is palette) # True — same object

Example 2: String Reassignment

greeting = "hello" alias = greeting greeting = greeting.upper() # .upper() returns a NEW string print(greeting) # "HELLO" (new object) print(alias) # "hello" (still the original) print(greeting is alias) # False — different objects

Example 3: Dict Mutation in a Function

def add_key(d): d["new"] = 99 # mutation: modifies the passed dict data = {"a": 1} add_key(data) print(data) # {"a": 1, "new": 99} ← changed outside!

Common Mistakes

Thinking += Always Mutates
x += 1 with an int is reassignment, not mutation. Only mutable types mutate with +=.
Confusing Name and Object
Saying "x is 5" instead of "x points to an object whose value is 5." Variables are labels, not containers.
Unexpected Aliasing
Writing 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)

Question 2 of 4

After running x = 10; x = x + 5, which statement is true about x?

Question 3 of 4

Which of the following is an example of mutation, not reassignment?

Question 4 of 4

What is the value of original after this code?
original = [1, 2]; copy = original; copy = [3, 4]