Variables, Objects, and References
"Names refer to objects. Names are introduced by name binding operations."
— Python Language Reference
Introduction
One of the biggest differences between Python and languages such as C or C++ is how variables work.
Many beginners think a variable stores a value.
In Python, that mental model is inaccurate.
A Python variable does not contain the object itself.
Instead, a variable is simply a name (reference) that points to an object stored somewhere in memory.
Understanding this concept is essential because it explains:
- Why assigning one variable to another doesn't create a copy.
- Why modifying a list affects other variables.
- Why immutable objects behave differently.
- Why shallow and deep copies exist.
- Why function arguments sometimes modify the original object.
These concepts appear repeatedly in technical interviews.
The Mental Model
Think of memory as a warehouse.
Objects live inside the warehouse.
Variables are labels attached to those objects.
flowchart TD
x --> I["10"]
The variable x doesn't contain 10.
It simply points to the integer object.
Creating an Object
Python performs several steps behind the scenes:
- Creates (or reuses) the integer object
10. - Creates the variable
x. - Makes
xreference that object.
flowchart LR
x --> I["10"]
Notice that the object exists independently of the variable.
Multiple Variables Can Reference the Same Object
Memory now looks like this.
flowchart LR
x --> I["10"]
y --> I
Both variables reference the same object.
Python does not create another integer.
Reassignment Does Not Modify the Object
Suppose we write:
Many beginners expect y to become 20.
It doesn't.
Instead:
flowchart LR
subgraph Before
x1["x"] --> I1["10"]
y1["y"] --> I1
end
subgraph After
x2["x"] --> I2["20"]
y2["y"] --> I3["10"]
end
The integer 10 was never modified.
The variable x simply started pointing somewhere else.
Everything in Python Is an Object
Almost everything you use in Python is an object.
Integers.
Strings.
Lists.
Functions.
Classes.
Even modules.
Everything is represented as an object.
Variables Are Just Names
Consider this code.
Memory
flowchart LR
a --> L["[1, 2, 3]"]
b --> L
Only one list exists.
Both variables point to it.
Modifying the Object
Now suppose we do this.
Result
Why?
Because there is only one list.
Both variables reference the same object.
Reassigning the Variable
Now instead write:
Memory
flowchart LR
a --> L1["[100]"]
b --> L2["[1, 2, 3, 4]"]
A new list was created.
Only a points to it.
The original list still exists because b references it.
Pythonic Example
Output
Common Interview Question
What is the output?
Output
No copy was made.
Another Example
Output
Why?
Because integers are immutable.
x = 20 creates a different reference.
Visual Comparison
Mutable Object
flowchart LR
a --> L["[1, 2]"]
b --> L
Modify list
↓
Both variables observe the change.
Immutable Object
flowchart LR
x --> I["10"]
y --> I
Reassign
flowchart LR
x --> I1["20"]
y --> I2["10"]
No object changed.
Only the reference changed.
Why This Matters in DSA
Many interview bugs happen because candidates accidentally modify shared objects.
Example
Many expect
Instead
Every row references the same list.
We'll revisit this in the Mutable vs Immutable chapter.
Common Interview Problems
Understanding references helps with:
- Copy List
- Clone Graph
- Deep Copy
- Merge Intervals
- Matrix problems
- DFS
- BFS
- Dynamic Programming memoization
Common Mistakes
Mistake 1
Thinking variables store values.
Incorrect mental model.
Variables store references.
Mistake 2
Expecting assignment to create copies.
No copy.
Mistake 3
Confusing reassignment with mutation.
This creates a new object.
It does not modify the previous one.
Best Practices
- Remember that assignment never creates a copy.
- Be careful when passing mutable objects to functions.
- Use
.copy()or thecopymodule when necessary. - Draw memory diagrams if you're unsure what happens.
Key Takeaways
- Variables are names.
- Objects live in memory.
- Variables reference objects.
- Assignment copies references, not objects.
- Reassignment changes the reference.
- Mutation changes the object.
- Understanding references makes later topics such as copying and mutability much easier.
Related Topics
- Python Memory Model
- Mutable vs Immutable Objects
- Assignment vs Copying
- Equality vs Identity
Practice Questions
- What is the difference between a variable and an object?
- Why does
bchange whena.append()is called? - Why doesn't
ychange afterx = 20? - Draw the memory diagram for:
- What happens when multiple variables reference the same object?
Summary
Python variables are references, not containers.
Once you understand this simple idea, many seemingly strange Python behaviors become completely logical.
This mental model forms the foundation for the next topics, where we'll explore how Python manages memory, why mutable and immutable objects behave differently, and how copying actually works.