Variables & References¶
In Python, variables don't store values directly — they store references (bindings) to objects in memory.
This single idea explains a surprising amount of Python's behavior: assignment, function arguments, mutable objects, copying, memory management. Once it clicks, a lot of "weird" Python behavior stops being weird.
Variables are References¶
Most people read this as "a stores the integer 10." That's not quite what happens. Python creates (or reuses) an integer object 10, and a is bound to it:
a isn't the object — it's just a name that points to one.
Multiple Variables Can Reference the Same Object¶
Both variables point at the same integer object — Python doesn't create a second one. You can confirm this with id():
Same identity, both times.
Reassignment Changes the Reference¶
Python doesn't modify the object 10 — it rebinds a to a different object entirely.
Before:
After:
The object 10 was never touched. Only what a points to changed.
Modifying an Object vs Rebinding a Variable¶
Now mutate the list:
The list itself was modified in place, and since both variables reference that same object, both see the change:
Rebinding is Different¶
The original list is untouched — a now points at a completely different list.
Variables Don't Own Objects¶
Objects exist independently of whatever variables happen to point at them.
del a didn't delete the list — it just removed one reference to it. b still holds a reference, so the list is still alive. Only once the last reference disappears can Python reclaim the memory.
Variables, References and Memory¶
Keep this chain in mind whenever you're reasoning about what a piece of Python code actually does.
Examples¶
Example 1¶
Output:
Example 2¶
Output:
Example 3¶
Output:
Follow-up Questions¶
Are Python variables pointers?¶
Not really. They're names bound to objects — unlike C pointers, they can't do pointer arithmetic or touch memory directly.
Do variables store objects?¶
No, they store references to objects.
What happens when we execute a = b?¶
No new object gets created — both names end up pointing at the same one.
What happens during reassignment?¶
The variable gets rebound to a different object. The original object is left alone.
Does del delete an object?¶
No. It removes a reference. The object itself only goes away once no references to it remain.
Why does modifying a list affect another variable pointing to it?¶
Because both variables reference the same underlying object, and the mutation happens on that object in place.
Common Interview Mistakes¶
❌ Saying variables store values.
✔ Variables store references to objects.
❌ Saying a is an object.
✔ a is a name. The object is whatever it references.
❌ Thinking a = b copies an object.
✔ It copies the reference — not the object.
❌ Thinking del immediately destroys an object.
✔ del removes a reference. The object survives until nothing references it anymore.
Key Takeaways¶
- Variables are names bound to objects, not the objects themselves.
- Multiple variables can reference the same object.
- Assignment copies the reference, not the object.
- Reassignment changes the reference, not the object.
- Modifying an object is different from rebinding a variable.
delremoves a reference — not necessarily the object.