Correct way to visualize variables in Python
If you’ve ever taken a beginner programming course, you’ve probably encountered the concept of variables. And if you were taught to think of variables as “boxes” containing values or objects, it’s time for a rethink. That classic box metaphor might actually be leading you astray. Instead, picture variables as sticky notes — here’s why this metaphor makes a lot more sense.

Why Sticky Notes Are a Better Metaphor
Think of variables not as boxes that store values but as sticky notes that point to the values. This approach can clear up some common misunderstandings. Let’s break it down with an example:
a = [2, 4, 6]
b = a
a.append(8)
print(b)
Here’s what’s happening in this code:
Create a List: We start by creating a list
[2, 4, 6]
.Attach a Sticky Note: We assign this list to the variable
a
. Imagine sticking a note labeleda
onto the list.Duplicate the Label: When we set
b = a
, we’re not copying the list but simply attaching another sticky note labeledb
to the same list.Modify the List: Adding
8
to the list througha
affects the list itself, not just the label.Print the List: Printing
b
reveals[2, 4, 6, 8]
becauseb
is pointing to the same list asa
.
Had we stuck with the box metaphor, b = a
might imply copying the contents of box a
into box b
. But with sticky notes, we understand that b
and a
are both pointing to the same object.
A More Realistic Example
Let’s consider a real-world scenario:
class Car:
def __init__(self, color):
self.color = color
print("There's a need for speed!")
my_car = Car("red")
friend_car = my_car
my_car.color = "blue"
print(friend_car.color)
There's a need for speed!
blue
In this example:
Define a Car Class: We start by defining a class
Car
.Instantiate a Car Object: We create a
Car
object with the color"red"
and assign it tomy_car
. The variablemy_car
is now a label pointing to thisCar
object.Duplicate the Label: We assign
my_car
tofriend_car
, so both labels point to the sameCar
object.Modify the Object: Changing
my_car.color
to"blue"
also updatesfriend_car.color
because both labels refer to the sameCar
object.
Variables and Object Creation
Consider this code snippet:
our_car = Car("green") * 4
You might encounter this error:
There's a need for speed!
TypeError: unsupported operand type(s) for *: 'Car' and 'int'
This error occurs because we tried to multiply a Car
object by an integer, which isn't allowed. More interestingly, the message "There's a need for speed!"
is printed, indicating that the Car
object was instantiated before the error occurred.
The variable our_car
isn’t created because the error happened during the evaluation of the right-hand side of the assignment. In Python, when you see an assignment, the right-hand side is evaluated first—this is where the object is created or retrieved. Only after that does the variable on the left get bound to this object.
So, next time you work with variables, remember: they’re not boxes but sticky notes attaching labels to objects. This perspective will help you understand how references work and avoid common misconceptions.