- This topic is empty.
-
AuthorPosts
-
February 16, 2026 at 1:10 am #6078
Understanding Shared Objects with the Student Example
πΉ Initial Context
While learning Object-Oriented Programming (OOP) in Python, we often write code like this:
class Student: def __init__(self, exercises): self.exercises = exercisesAnd then store objects in a dictionary:
students = {} students["Alice"] = Student(12) students["Bob"] = Student(7)Each key stores a reference to a
Studentobject.A common question learners ask is:
βIf
Student(12)already exists, can I reuse it for other students who also have 12 exercises?βThis tutorial explains what happens when you reuse objects β and when you should avoid doing so.
πΉ Step 1: Creating One Object and Reusing It
Letβs create one object first:
s = Student(12) students["Alice"] = s students["Charlie"] = sNow both names refer to the same object.
Internally:
"Alice" β s "Charlie" β sThere is only one
Studentobject in memory.
πΉ Step 2: What Happens When You Modify It?
Now change Aliceβs record:
students["Alice"].exercises = 20 print(students["Charlie"].exercises)Output:
20Why?
Because both names point to the same object.
You did not create two students β you created one student with two labels.
πΉ Step 3: Visual Memory Model
Think of memory like this:
Object in Memory
Object S β Student(exercises=12)Dictionary
"Alice" β Object S "Charlie" β Object SAfter update:
Object S β Student(exercises=20)Both keys now show the updated value.
πΉ Step 4: Creating Separate Objects with Same Values
Usually, this is what you want:
students["Alice"] = Student(12) students["Charlie"] = Student(12)Now:
"Alice" β Student(12) (Object A) "Charlie" β Student(12) (Object B)They look the same, but they are different objects.
πΉ Step 5: Identity vs Value (
isvs==)Try this:
a = Student(12) b = Student(12) print(a is b) print(a == b)Output:
False FalseExplanation:
isβ checks if both variables point to the same object==β checks if values are equal (not implemented here, so False)
They are different objects in memory.
πΉ Step 6: Using
id()to See Memory AddressesYou can see this clearly with
id():a = Student(12) b = Student(12) print(id(a)) print(id(b))The numbers will be different β showing two separate objects.
But with shared objects:
s = Student(12) x = s y = s print(id(x)) print(id(y))They will be the same.
πΉ Step 7: When Is Sharing an Object Useful?
Sharing objects is useful when the object represents something common.
Examples:
β App configuration
β Global counters
β Shared settings
β Cache objectsExample:
config = Config() users["Alice"] = config users["Bob"] = configHere, sharing is intentional.
πΉ Step 8: When Should You NOT Share Objects?
Do NOT share when the object represents:
β People
β Accounts
β Profiles
β Records
β ContactsEach entity should have its own object.
Example (correct way):
students["Alice"] = Student(12) students["Bob"] = Student(12)
πΉ Step 9: Real-World Analogy
Imagine two students score 12 marks.
Do they become the same person?
No.
They just share a value.
Python works the same way.
Same value β Same object.
πΉ Step 10: Common Beginner Mistake
Mistake:
s = Student(0) for name in names: students[name] = s # β WrongResult: All students share one record.
Correct:
for name in names: students[name] = Student(0) # β CorrectEach gets their own object.
β Final Takeaway
In Python:
Dictionaries store references to objects.
If multiple keys point to the same object, they share data.
Remember:
β Reusing object = shared state
β New object = independent state
β Most real-world entities need separate objectsUnderstanding this helps you avoid serious logical bugs in larger programs.
-
AuthorPosts
- You must be logged in to reply to this topic.

