__slots__
say it: “DUN-der SLOTS” /ˈdʌndər ˈslɑts/
Lists the only attributes a class's objects may have, saving memory.
What does __slots__ do?
__slots__ lists the only attributes a class's objects may have. They use less memory, which adds up when you make millions of them.
class Pixel:
__slots__ = ("x", "y", "color")
def __init__(self, x, y, color):
self.x, self.y, self.color = x, y, color
p = Pixel(0, 0, "red")
p.size = 3 # AttributeError: not in __slots__
Level: Master · More Python terms at this level
Updated