Slots vs Dict Memory
Advanced · Write the code · from How Python Works Under the Hood: Memory, GIL, and Bytecode
From the lesson: How Python Works Under the Hood: Memory, GIL, and Bytecode
Every time you type x = 42, a lot happens behind the scenes. Python creates an integer object in memory, assigns a reference to it, and manages its lifecycle automatically. Understanding these internals doesn't just satisfy curiosity -- it helps you write faster code, debug memory issues, and nail technical interviews.
Read the lessonCreate two classes:
1. PersonDict with __init__(self, name, age) that sets self.name and self.age (uses default __dict__)
2. PersonSlots with __slots__ = ['name', 'age'] and the same __init__
Create one instance of each. Print whether each has a __dict__ attribute using hasattr(). Expected output:
PersonDict has __dict__: True
PersonSlots has __dict__: FalseMore Internals exercises
- Disassemble a FunctionAdvanced
- Predict Integer CachingAdvanced
- Count ReferencesAdvanced
- Fix the Identity BugAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz