Create a Slotted Class
Beginner · Write the code · from Python __slots__: Reduce Memory Usage by 40%
From the lesson: Python __slots__: Reduce Memory Usage by 40%
Imagine a parking lot. A normal Python object is like an open field where cars can park anywhere. Flexible, but wasteful - you need a huge space even if only a few cars show up.
Read the lessonCreate a class called Pixel with __slots__ for three attributes: x, y, and color. Write an __init__ method that accepts all three. Create a Pixel at position (10, 20) with color 'red' and print its color.
# Create the Pixel class with __slots__
# Create a pixel and print its color
More __slots__ exercises
- Predict the Output: Slots RestrictionBeginner
- Fix the Bug: Missing SlotIntermediate
- Slots with InheritanceAdvanced
- Compare Memory: Regular vs SlottedAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz