Each python object has a __dict__
atttribute which is a dictionary containing all other attributes. e.g. when you type self.attr
python is actually doing self.__dict__['attr']
. As you can imagine using a dictionary to store attribute takes some extra space & time for accessing it.
However, when you use __slots__
, any object created for that class won't have a __dict__
attribute. Instead, all attribute access is done directly via pointers.
So if want a C style structure rather than a full fledged class you can use __slots__
for compacting size of the objects & reducing attribute access time. A good example is a Point class containing attributes x & y. If you are going to have a lot of points, you can try using __slots__
in order to conserve some memory.