Variables are pointers 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.9—3.13

Sometimes Python programmers think of variables as kind of like a bucket that might hold an object. Unfortunately, this mental model of Python breaks down pretty quickly.

Variables in Python are not buckets that contain things, but pointers: variables point to objects.

Also see the variable definition in Python Terminology and see the much longer article, Variables and objects in Python.

Changing two lists at once...?

Here we have a variable a that points to a list:

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

Let's make a new variable b and assign it to a:

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

If we append a new item to b, what will its length be?

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

Initially, the b list had four items, so now it should have five items. And it does:

>>> len(b)
5

How many items do you think a has? What's your guess?

>>> len(a)

Is it five, the same as b? Or is it still four, as it was before?

The a list also has five items:

>>> len(a)
5

What's going on here?

Well, the variables a and b, both point to the same list.

If we look up the unique ID for the object that each of these variables points to, we'll see that they both point to the same object:

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

This is possible because variables in Python are not buckets, but pointers.

Variables are separate from objects

Let's say we've made three assignment statements to three variables:

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

This is how Python represents these variables and objects:

Diagram showing variables on the left and objects on the right, with arrows between each. The "a" and "b" variables point to the same list. The "c" variable points to a different list, which happens to have the same values as the first list.

Note that Python stores the variables in a separate place than it stores the objects.

At any given time, each variable in Python points to exactly one object. But each object may have multiple variables pointing to it.

In our case, the variables a and b, both point to the same object!

How did this happen, and how can we avoid it?

Assignment statements don't copy

Assignment statements in Python don't copy anything. Assignments just point a variable to an object.

So assigning one variable to another will point two variables to the same object.

When we said b = a, we gave two names to the same object:

>>> b = a

That line of code is a weird one to see when we're working with objects that can be changed. We're working with list objects and lists are mutable, meaning they can be changed.

Explicitly copying a list

If we wanted to make a new variable that stores a new list, there are lots of ways to do it.

We could use slicing to copy a, and then point b to the new list:

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

Or we could use the list constructor to loop over a and make a new list out of it:

>>> b = list(a)

Or we could call the list copy method:

>>> b = a.copy()

All of these assignments involve explicitly making a new list, and then pointing b to that new list.

Variables are like pointers, not buckets

An assignment in Python points a variable to an object. That's it!

Variables don't contain objects, and nothing gets implicitly copied during an assignment.

Instead of thinking of variables as buckets that contain objects, think of them as bindings, references, aliases, or pointers. These are all terms that you'll hear used to describe how variables work in Python. Regardless of what word you use to describe the relationship between variables and objects, make sure not to use the mental model of a variable containing an object.

Variables are not like buckets in Python. Instead, they're pointers.

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%
Deepen your Python skills

We learn through practice. Sign up to Python Morsels to practice Python every single week.

Each exercise is based on your Python skill level, from novice to advanced.