Assignment vs. Mutation in Python

Share
Copied to clipboard.
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
3 min. read Watch as video Python 3.10—3.14

Let's talk about the two different types of change in Python.

Also see the assignment and mutation definitions in Python Terminology and see the much longer article, Variables and objects in Python.

Remember: variables are pointers

When talking about Python code, if I say "we changed X", there are two different things that I might mean.

Let's say we have two variables that point to the same value:

>>> a = [2, 1, 3, 4]
>>> b = a

Remember that variables in Python are pointers. That means that two variables can point to the same object. That's actually what we've done here.

Let's change the object that the variable b points to.

Mutating a list

If we append a number to the list that b points to, we'll see that the length of b is now five:

>>> b.append(7)
>>> len(b)
5

What's the length of a now?

>>> len(a)

We appended an item to b, and its length grew. Has a grown also?

Does it have five items in it? Or does it still have four items in it?

Well, a also has five items:

>>> len(a)
5

This happened because when we assigned b to a, we pointed two variables to the same object. So changing that object will change the value that both variables point to.

Mutation

Tuples, numbers, and strings cannot be changed after they've been created. But lists, dictionaries, and sets can be.

Objects that can be changed are called mutable. The act of changing a mutable object is called a mutation.

Assignment

So when we appended to the list that b points to, we changed that list. But we also saw that change reflected in a, because the variables a and b both happen to point to the same list:

>>> a
[2, 1, 3, 4, 7]
>>> b
[2, 1, 3, 4, 7]

Currently, a and b both point to the same object:

>>> id(a)
140534104117312
>>> id(b)
140534104117312

Let's say we change the value of b with an assignment statement:

>>> b = [3, 5, 6]

There are now only three items in b:

>>> len(b)
3

But how many items are there in a?

>>> len(a)

Are there five items as there were before? Or are there are only three items now? What's your guess?

The list a still has five items in it, so a is unchanged:

>>> len(a)
5

The variables a and b originally pointed to the same list:

>>> a
[2, 1, 3, 4, 7]
>>> b
[2, 1, 3, 4, 7]

But then we assigned the variable b to a new object:

>>> b = [3, 5, 6]

That didn't affect a at all. Our a variable still points to the original list:

>>> a
[2, 1, 3, 4, 7]

Assignments versus mutations

Python has two distinct types of change.

Assignment changes a variable. That is, it changes which object a variable points to.

Mutation changes an object, which any number of variables may be pointing to.

So we can change variables through an assignment, and we can change the objects that those variables point to through a mutation.

Changing variables and changing objects

When I say "we changed x", I could mean two very different things.

I might mean that we mutated the object that x currently points to. Or I might mean that we assigned x to a new object (leaving whatever object it might have pointed to before unchanged).

Mutations change objects, while assignments change variables (that is, they change which object of variable points to).

Series: Assignment and Mutation

Python's variables aren't buckets that contain things; they're pointers that reference objects.

The way Python's variables work can often confuse folks new to Python, both new programmers and folks moving from other languages like C++ or Java.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.