Editor

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 lesson

Create 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

All Python exercises · 50 practice problems · Object-Oriented Programming quiz