- This topic is empty.
-
AuthorPosts
-
February 18, 2026 at 3:20 am #6092
—
Understanding Value Types with the Student Example
๐น Initial Context
While learning Object-Oriented Programming (OOP) in Python, we often use dictionaries to store objects.
For example:
class Student: def __init__(self, exercises): self.exercises = exercises students = {} students["Alice"] = Student(12) students["Bob"] = Student(7) students["Harry"] = Student(5)Here, every value in the dictionary is a
Studentobject.
This is clean, predictable, and easy to manage.However, beginners often wonder:
โCan I store other types, like numbers or strings, in the same dictionary?โ
Letโs explore the answer.
๐น Step 1: Python Allows Mixed Types in Dictionaries
In Python, dictionary values can be any data type.
This is valid code:
students = {} students["Alice"] = Student(12) # Object students["Bob"] = 50 # Integer students["Harry"] = "Absent" # String students["Emma"] = [1, 2, 3] # ListPython will not give any error.
Why?
Because Python is dynamically typed.
Variables and containers do not enforce fixed types.
๐น Step 2: What Happens Internally
Internally, the dictionary looks like this:
"Alice" โ Student object "Bob" โ Integer (50) "Harry" โ String ("Absent") "Emma" โ ListEach key simply points to some object in memory โ Python does not care what type it is.
๐น Step 3: Why Mixed Types Can Cause Problems
Now imagine this code:
for name in students: print(students[name].exercises)If
students["Bob"]is an integer:โ Program crashes:
AttributeError: 'int' object has no attribute 'exercises'Because integers do not have
.exercises.This makes your program unreliable.
๐น Step 4: Example of a Bug Caused by Mixed Types
Consider:
students["Bob"] = 50 print(students["Bob"].exercises)Result:
AttributeErrorThis happens at runtime, not during coding โ which makes it harder to debug.
๐น Step 5: Best Practice โ Keep Dictionary Values Consistent
In most real programs, you should keep one type per dictionary.
Good design:
students: dict[str, Student] = {} students["Alice"] = Student(12) students["Bob"] = Student(7) students["Harry"] = Student(5)Now you can safely assume:
Every value is a
Student.This makes code easier to read and maintain.
๐น Step 6: When Mixed Types Are Actually Useful
Sometimes, mixed types are intentional.
Example: Configuration settings
config = { "debug": True, "port": 8080, "host": "localhost", "timeout": 30.5 }Here, different types make sense.
Another example: JSON data from APIs.
๐น Step 7: How to Handle Mixed Types Safely
If mixed types are unavoidable, check types first:
value = students["Bob"] if isinstance(value, Student): print(value.exercises) else: print("Not a Student object")This prevents crashes.
๐น Step 8: Using Type Hints to Avoid Problems
Modern Python supports type hints:
students: dict[str, Student] = {}This tells tools and editors:
This dictionary should only contain
Studentobjects.It helps catch mistakes early.
๐น Step 9: Real-World Analogy
Imagine a cabinet labeled โStudent Recordsโ.
Inside:
- Folder โ Student file โ๏ธ
- Folder โ Shopping bill โ
- Folder โ Student file โ๏ธ
Technically possible.
Practically confusing.Good systems keep things organized.
โ Final Takeaway
Yes, Python dictionaries can store mixed data types:
students["Alice"] = Student(12) students["Bob"] = 50But good programming practice is:
โ Keep values consistent
โ One dictionary โ one main data type
โ Avoid runtime errors
โ Improve readabilityRule of Thumb
If your dictionary represents โstudentsโ,
store onlyStudentobjects in it.
-
AuthorPosts
- You must be logged in to reply to this topic.

