Python Slots Inheritance
Posted : admin On 4/1/2022Slots
Contains iterable of instance data attributes which are only allowed to be accessed(set/get/delete).
Inheritance in Python. Inheritance is a powerful feature in object oriented programming. It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class. No deposit free Python Inheritance Slots spins Python Inheritance Slots are subject to 20x wagering requirements. Free spins winnings are capped at $100. The no deposit spins will be valid for 7 days. A minimum deposit is required to claim each match bonus and extra spins Python Inheritance Slots bonus.
- Instance methods, class methods, static methods, class variable are not affected by slots.
- When slots are used __dict__ and __weakref__ is not created for objects. __dict__ and __weakref__ can be enabled by explicitly specifying in iterable passed to __slots__.
- __slots__ uses less memory to store attributes and provides faster attribute lookup compared to __dict__.
Slots and Inheritance Each class in single inheritance hierarchy can have their own slots and they are combined in single set from inheritance tree to check for validation when accessing attribute. When inheriting class with slots, subclass slots is not required to.
How __slots__ uses less memory?
- When using __dict__ python allocates new namespace dictionary to each instance of class, so that attribute can be assigned per instance base which can be expensive in terms of memory usage.
- while using slots python shares single __slots__ attribute with all instances of class and when storing instance data attribute value fixed amount of memory is allocated as number of instance data attribute to be stored are fixed.
Inspecting __dict__ of Class and Object
Output
Defining slots and inspecting __slots__ of Class and Object
Output
- when __slots__ is defined , Objects of class does not have the __dict__ attribute
Output
Copy
- Setting instance data attribute not defined in __slots__
Output
__slots__ and __int__() method
If an instance data attribute name is not defined in __slots__, it can not be initialized using __init__ method.
Output
Slots with Instance method, Class Method, static Method and Class Variable
Only Instance data attribute are affected by __slots__, Instance methods, class methods, static methods, class variable are not affected by slots.
Output
Copy
Using Different iterables to initialize Slots
Any iterable can be used to initialize __slots__ attribute except string.
Assigning tuple to __slots__
Copy
Assigning list to __slots__
Copy
Assigning set to __slots__
Copy
Assigning dict to __slots__
Copy
Changing slots value after defining class
Changing __slots__ value after defining class does not affect the Class definition, when classobject is created it creates class level descriptors for each instance data attribute which are present in iterable at the time of class definition creation.
Copy
Slots attributes and Class attributes
Python uses Descriptors to define each attribute in slots iterable. So attribute name defined in slots can not be used as class variable name.
Copy
Using __dict__ with __slots__
'__dict__' can be added as an attribute to slots in order to enable attribute namespace dictionary.
Output
Copy
Slots and Inheritance
- Each class in single inheritance hierarchy can have their own slots and they are combined in single set from inheritance tree to check for validation when accessing attribute.
- When inheriting class with __slots__, subclass __slots__ is not required to include attributes defined in superclass.
Output
- Redefining the __slots__ attribute which are present in superclass will waste memory space.
Output
Slots and Multiple Inheritance
When using multiple inheritance only one class is allowed to have __slots__.
Multiple inheritance with more than one parent defining slots is not supported, and will result in TypeError: multiple bases have instance lay-out conflict.
Output
Subclass without __slots__ whose superclass is having __slots__
When Subclass does not defines __slots__ with superclass having __slots, superclass's __slots__ becomes class attribute to subclass and by default __dict__ and __weakref__ is enable for subclass.
Output
Subclass with __slots__ whose superclass does not have __slots__
When Subclass defines __slots__ and it's super class does not have __slots__ defined, __dict__ attribute of super class is inherited and use of __slots__ becomes meaningless.
Output
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.
On a sidenote, you might want to give PyPy a try. It does all of theseoptimizations by default.
Python Slots Inheritance Rules
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