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.
variabledefinition in Python Terminology and see the much longer article,
Variables and objects in Python.
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.
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:
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 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.
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.
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.
You don't learn by watching videos or reading. You learn by writing Python code!
Sign up to Python Morsels to deepen your Python skills single week through hands-on Python exercises. Your weekly practice will be based on your skill level, from novice to advanced.
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.
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.
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!