Python Slots Vs Data Class
In this tutorial, we will try to understand slots in Python with a simple example.
Python has a reserved keyword known as “class” which you can use to define a new class. The object is a working instance of a class created at runtime. How to create a class in Python? There are some terms which you need to know while working with classes in Python. The “class” keyword 2. The instance attributes 3. Let’s learn slots in Python with a simple piece code. slots in Python. In the above program, slots is used in the class Right only once declaring a particular slot one time in the class. While the opposite happened in class Left thus declaring three slots at one time. Thus the size of the class Right is 36 which is lesser than 44.
Python Slots Vs Data Classification
In Python, we use __dict__ function to store the object attributes. This allows setting new attributes at runtime.
The function __dict__ acts as a dictionary. It doesn’t have a fixed number of attributes stored. One can add attributes to the dictionary after defining them but dynamic adding of attributes to a class is not possible in built-in classes like ‘int’ or ‘list’ etc.
However the above function takes up lesser spaces but also, this leads to the function taking up a lot of space in RAM(if thousands and millions of objects are there).
To solve this limitation, we have __slots__ in Python. It provides a static structure not allowing adding attributes after the creation of an instance. __slots__ saved about 9GB RAM.
To define __slots__, one has to define a list with the name ___slots__ which would contain all the attributes to be in use.
Let’s learn slots in Python with a simple piece code.
__slots__ in Python
NOTE: ONE SHOULD DECLARE A PARTICULAR SLOT ONE TIME IN A CLASS. AN ERROR WON’T OCCUR IF THIS IS NOT FOLLOWED BUT OBJECTS WILL TAKE UP MORE SPACE THAN THEY SHOULD
(36,44)
In the above program, __slots__ is used in the class Right only once declaring a particular slot one time in the class. While the opposite happened in class Left thus declaring three slots at one time. Thus the size of the class Right is 36 which is lesser than 44 (size of class Left).
In the above programs, the line “__slots__= ……..” is to define the slots and “def __init__” we define or instantiate the values.
Also lastly, __slots___ was actually created for faster attribute access.
Leave a Reply
You must be logged in to post a comment.
In Python every class can have instance attributes. By default Pythonuses a dict to store an object’s instance attributes. This is reallyhelpful as it allows setting arbitrary new attributes at runtime.
However, for small classes with known attributes it might be abottleneck. The dict
wastes a lot of RAM. Python can’t just allocatea static amount of memory at object creation to store all theattributes. Therefore it sucks a lot of RAM if you create a lot ofobjects (I am talking in thousands and millions). Still there is a wayto circumvent this issue. It involves the usage of __slots__
totell Python not to use a dict, and only allocate space for a fixed setof attributes. Here is an example with and without __slots__
:
Without__slots__
:
With__slots__
:
The second piece of code will reduce the burden on your RAM. Some peoplehave seen almost 40 to 50% reduction in RAM usage by using thistechnique.
Python Class Data Type
On a sidenote, you might want to give PyPy a try. It does all of theseoptimizations by default.
Below you can see an example showing exact memory usage with and without __slots__
done in IPython thanks to https://github.com/ianozsvald/ipython_memory_usage