Python Variables Demystified: What's in a Name?
If you've been using Python for a while, you know how to assign a variable. It’s one of the first things we learn: x = 10. But have you ever stopped to ask what really happens when you run that line?
For intermediate and senior developers, understanding this mechanism is the master key that unlocks Python's behavior around mutability, memory management, and function arguments. So, let's peel back the first layer. 🧅
Variables are Names, Not Boxes
The most common mental model—a variable as a box holding a value—can lead to confusion in Python. A more accurate analogy is that of a name tag or sticky note.
In Python, a variable is a name (or label) that is bound to an object living somewhere in your computer's memory.
When you write:
my_var = "Hello, World!"
You are not putting the string into a box called my_var. You are creating the string object "Hello, World!" in memory and then attaching the name tag my_var to it.
Identity: The Object's True Self
Every object in Python has a unique identity, a sort of permanent memory address. You can find it using the id() function. This identity is what the is keyword checks.
a = [1, 2, 3] # Create a list object, attach the name `a` to it.
b = a # Attach a new name `b` to the SAME object.
print(id(a)) # Output: e.g., 140247582398784 (this will change each run)
print(id(b)) # Output: 140247582398784 (Same as id(a)!)
print(a is b) # Output: True. They are the same object.
This shows that we have one list object with two different names. An action performed using one name will be visible through the other.
a.append(4)
print(b) # Output: [1, 2, 3, 4]
# Changing 'a' changed 'b' because they reference the same object.
Assignment vs. Equality: is vs. ==
This brings us to a critical distinction:
a is bchecks if two names point to the exact same object (identity check).a == bchecks if the objects the names point to have the same value (value check).
list1 = [1, 2, 3]
list2 = [1, 2, 3] # A new, separate list object with the same value.
print(list1 == list2) # True? Yes, their values are equal.
print(list1 is list2) # True? No! They are two different objects.
print(id(list1), id(list2)) # Their identities (memory addresses) are different.
What Happens During Assignment?
Let's trace through the process. The key is to think of the assignment operator = as the "sticky note applicator."
x = 1000
- The interpreter creates an integer object
1000in memory. - It applies a sticky note labeled
xto this object.
Now, what happens when we do:
y = x
- It does not create a new copy of the object
1000. - It simply applies a new sticky note labeled
yto the exact same object that thexsticky note is on.
We can visualize this relationship. Both names point to the same underlying object.
(Memory)
┌─────────────┐
│ int object │
│ value:1000│ <---(name tag "x")
└─────────────┘ <---(name tag "y")
Key Takeaway 🗝️
The fundamental concept to internalize is this: In Python, variables are references to objects, not containers for values.
This "names and bindings" model is the cornerstone of understanding Python. It explains why appending to a list passed into a function changes the original, and it's the reason for the different behavior you'll see in this challenge.
Speaking of which, test your understanding with this snippet! Based on what we've learned, what will the final output be? 💭
a = 500
b = a
a = a + 100
list1 = [1, 2]
list2 = list1
list1.append(3)
print(f"b is {b}")
print(f"list2 is {list2}")
Hint: The behavior is different for integers and lists. Why might that be? We'll answer that exact question in our next post on mutability and Immutability.
Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like Genius.


Comments
Post a Comment