Using __slots__ to Store Instance’s Attributes

Python
Author

Imad Dabbura

Published

December 8, 2022

Typically all attributes are stored in the instance’s dictionary __dict__. This will add space overhead due to the hash table being sparse. We can save memory by storing all the attributes in a tuple-like data structure using __slots__ at the beginning of the class definition (see below):

import sys


class C:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class C_Slots:
    __slots__ = ("x", "y")

    def __init__(self, x, y):
        self.x = x
        self.y = y

print(sys.getsizeof(C))         #=> 904
print(sys.getsizeof(C_Slots))   #=> 1,072

The main problems with __slots__:

In conclusion, use __slots__ only if it is justified.