__slots__ — Expert Playground
Restricts instance attributes to a fixed set for memory savings
Python Playground
# __slots__ internals
import sys
# Inspect how Python stores attributes
class Example:
class_var = "shared"
def __init__(self):
self.instance_var = "unique"
obj = Example()
# Class vs instance namespace
print("Class namespace keys:", list(Example.__dict__.keys())[:5])
print("Instance namespace:", obj.__dict__)
# Size comparison
print(f"\nClass dict size: {sys.getsizeof(Example.__dict__)} bytes")
print(f"Instance dict size: {sys.getsizeof(obj.__dict__)} bytes")
# Attribute lookup chain
print(f"\nMRO: {[c.__name__ for c in Example.__mro__]}")
Output
Click "Run" to execute your code
Understanding the internals helps with advanced debugging and framework development.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?