Python 101 : __slots__ and data management.

For classes with small, simple data, we could save memory by using the __slots__ attribute to the class. 

For example the Car class below:

With __slots__python uses a smaller internal structure to represent the data of the different instances. 

Normally python uses a  dictionary structure to hold the class data, but with __slots__, it uses a list or a tuple which are more economic that dictionaries.


  • With __slots__, we can't append other attributes to the class - we can only use the ones indicated in the __slots__ construct -.
  • Multiple inheritance is not supported when using __slots__.


Leave as a comment: