Memory, References & The Dot Operator
To master OOP, you must understand how Python manages memory. Most bugs in advanced Python apps occur because developers misunderstand how variables reference objects in RAM.
1. Learning Objective
By the end of this lesson, you will understand:
- How RAM is divided into Stack Memory and Heap Memory.
- What an Object Reference actually is (a memory pointer).
- What happens when two variables point to the exact same object (
s2 = s1). - How the Dot Operator (
.) navigates memory behind the scenes.
2. Why This Concept Exists
In lower-level languages like C, programmers manually allocate and free memory pointers.
Python manages memory automatically, but it uses Object References. If you don't realize that variables store memory addresses rather than the objects themselves, you will encounter unexpected bugs where modifying one variable silently alters data in another variable!
3. Common Beginner Confusions
Confusion 1: "When I write s2 = s1, doesn't Python duplicate the object?"
- Answer: NO!
s2 = s1does NOT copy the object in memory. It simply copies the memory address froms1intos2. Boths1ands2now point to the exact same physical object in Heap RAM.
Confusion 2: "What does the dot operator (s1.name) actually do?"
- Answer: The dot operator is a memory navigation tool. It tells Python: "Take the memory address stored inside
s1, go to that heap location, and look up the attribute keyname."
4. Mental Model: House Address Tag & Remote Control
-
House Address Tag (References):
- Imagine a house built on plot #402 (Heap Memory).
- Writing
s1 = Student()is like writing the address"Plot #402"on a sticky note (s1). - Writing
s2 = s1gives a second person (s2) another sticky note with the exact same address"Plot #402". If Person 2 paints the house blue, Person 1 sees a blue house too because there is only one physical house.
-
Remote Control Pointer (The Dot Operator):
- The variable
s1is a remote control aimed at a TV object. - Pressing
.volumeon the remote control sends a signal down the beam to adjust the volume on that specific TV object.
- The variable
5. Internal Python Execution Flow
Let's trace what Python does internally when executing reference assignment and attribute lookup:
Code: s1 = Student()
s2 = s1
s1.name = "Sai"
↓ Execution Steps:
1. s1 = Student()
- Python creates a Student object at Heap Address 0x8100.
- s1 receives value 0x8100.
2. s2 = s1
- Python reads value 0x8100 inside s1.
- s2 receives value 0x8100.
- Reference count for object at 0x8100 increases to 2.
3. s1.name = "Sai"
- Python looks up s1 -> finds Address 0x8100.
- Python opens object at 0x8100.
- Python creates attribute 'name' inside 0x8100 storing "Sai".
- Printing s2.name will output "Sai" because s2 points to 0x8100!
6. Memory Visualization: Multiple References to One Object
RAM Layout: Two Variables, One Object
STACK MEMORY
s1 => 0x8100
s2 => 0x8100
⇉
HEAP MEMORY (Single Object)
- Green (Heap): A single object memory block at
0x8100. - Purple (Stack): Both
s1ands2hold address0x8100.
7. Python Code Implementation
class Student:
pass
# Step 1: Create object and assign reference to s1
s1 = Student()
# Step 2: Assign s1 reference to s2 (No new object is created!)
s2 = s1
# Step 3: Use dot operator on s1 to set attributes
s1.name = "Sai"
s1.age = 21
# Step 4: Access attributes using s2
print("s2.name:", s2.name)
print("s2.age:", s2.age)
# Step 5: Check identity using 'is' operator
print("Do s1 and s2 point to same memory location?", s1 is s2)
Output:
s2.name: Sai
s2.age: 21
Do s1 and s2 point to same memory location? True
8. Line-by-Line Execution Analysis
Line: s1 = Student()
- Right side
Student(): Python creates an emptyStudentobject at Heap location0x8100. - Left side
s1 =: Python stores address0x8100insides1.
Line: s2 = s1
- Right side
s1: Python evaluatess1and retrieves value0x8100. - Left side
s2 =: Python stores address0x8100insides2. No new heap allocation occurs.
Line: s1.name = "Sai"
- Dot operator
s1.: Resolvess1to0x8100. - Attribute assignment
.name = "Sai": Attaches string"Sai"to the dictionary of object0x8100.
Line: print(s2.name)
- Dot operator
s2.: Resolvess2to0x8100. - Attribute lookup
.name: Finds"Sai"inside object0x8100and prints it.
9. Before / After Memory Visualization
Before Executing s2 = s1
s1→0x8100(Object count: 1 reference).s2does not exist.
After Executing s2 = s1
s1→0x8100.s2→0x8100.- Both variables share the exact same Heap address
0x8100.
10. Illustration Prompt for Diagram Generation
Excalidraw Diagram Prompt: A hand-drawn diagram illustrating Python memory references and the dot operator. Draw a Stack Memory column on the left with two purple boxes labeled "s1" and "s2". Put "0x8100" inside both boxes. Draw two blue arrows coming out of "s1" and "s2", both converging onto a single large green box on the right labeled "Heap Memory: 0x8100 (Student Object)". Draw a magnifying glass icon next to a dot labeled "Dot Operator (.)", showing how it resolves variable "s1" down the arrow to read attribute "name: Sai" inside the green box. Clean white background, hand-drawn look, minimal palette (Purple, Green, Blue, Orange).
Quick Summary
- Variables in Python store Memory Address References, not actual object data.
s2 = s1copies the reference address, creating a second pointer to the same physical object.- The Dot Operator (
.) is a navigation mechanism that resolves a reference to its heap memory location. - Modifying an object through one reference affects all other references pointing to that same object.