- This topic is empty.
-
AuthorPosts
-
February 20, 2026 at 11:30 am #6111
📘 Understanding Dictionaries with Objects in Python
When learning Python, most beginners first see dictionaries storing simple values like numbers or lists. But dictionaries can also store objects — and this is where programming becomes much more powerful.
In today’s lesson, we’ll understand:
- How a dictionary can store objects
- Why
ExerciseCounter()appears twice in a dictionary - How each student gets their own independent counter
- What actually happens in memory
🧠 The Goal
We want to track how many exercises each student completes.
Instead of storing numbers directly in a dictionary, we’ll store objects.
🔹 Step 1: Create the Class
class ExerciseCounter: def __init__(self): self.__exercises = 0 def done(self): self.__exercises += 1 def how_many(self): return self.__exercisesWhat This Class Does
__init__()→ Initializes exercise count to 0done()→ Adds one completed exercisehow_many()→ Returns the total count
Each object created from this class has its own private counter.
🔹 Step 2: Dictionary with Objects as Values
students = { "peter": ExerciseCounter(), "sarah": ExerciseCounter() }This line confuses many beginners.
Let’s break it down carefully.
🔍 What Actually Happens Here?
When Python reads:
"peter": ExerciseCounter()It:
- Creates a NEW
ExerciseCounterobject - Stores it in memory
- Assigns it to the key
"peter"
Then Python reads:
"sarah": ExerciseCounter()It:
- Creates ANOTHER NEW object
- Stores it separately in memory
- Assigns it to
"sarah"
Important Rule
Every time you write:
ExerciseCounter()Python creates a brand new object.
Even if the code looks the same, they are different objects.
🔹 Visual Memory Diagram
After execution:
students (dictionary) │ ├── "peter" ───▶ ExerciseCounter object A │ └── __exercises = 0 │ └── "sarah" ───▶ ExerciseCounter object B └── __exercises = 0Two keys.
Two separate objects.
Two independent counters.
🔹 Step 3: Updating Exercise Counts
Now let’s increase Peter’s exercises:
students["peter"].done() students["peter"].done()Now:
Peter → 2 Sarah → 0If we increase Sarah:
students["sarah"].done()Now:
Peter → 2 Sarah → 1They remain independent because they are separate objects.
🔬 Proving They Are Different Objects
You can verify this using
id():print(id(students["peter"])) print(id(students["sarah"]))The two memory addresses will be different.
That proves they are not the same object.
❌ Common Mistake (Shared Object)
This is wrong:
counter = ExerciseCounter() students = { "peter": counter, "sarah": counter }Now both keys point to the same object.
If Peter does an exercise:
students["peter"].done()Sarah’s count will increase too — because it’s the same object.
🔹 Why Use Objects Instead of Numbers?
You could write:
students = { "peter": 0, "sarah": 0 }But objects are better because:
✔ You can add more behavior
✔ You can protect data
✔ You can expand functionality later
✔ Code becomes more readableExample of extension:
def reset(self): self.__exercises = 0No need to change main program logic.
🔹 Dictionary vs List
This is NOT a list of objects.
A list would look like:
[ExerciseCounter(), ExerciseCounter()]But then you wouldn’t know which object belongs to which student.
A dictionary gives us:
Name → ObjectThat mapping is powerful.
🔹 Key Concept to Remember
A dictionary:
Key → ValueAnd the value can be anything:
- int
- string
- list
- another dictionary
- object
In this case:
Key → ExerciseCounter object
🎯 Final Takeaway
When you write:
students = { "peter": ExerciseCounter(), "sarah": ExerciseCounter() }You are creating:
- One dictionary
- Two separate objects
- Each object stores its own exercise count
- Each student “remembers” their own progress
Objects store the data.
The dictionary stores references to those objects.That’s how Python keeps everything separate and organized.
-
AuthorPosts
- You must be logged in to reply to this topic.

