Slots in python dict
Posted on March 1, 2020 • One minute • 158 words
The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have. The benefits of using __slots__ are as follows:
- faster attribute access.
- space savings in memory.
The space savings is from the following:
- Storing value references in slots instead of __dict__
- Denying __dict__ and __weakref__ creation if parent classes deny them and you declare __slots__
Example:
class Person(object):
__slots__ = ('a', 'b')
Now, you can’t have dynamic properties in person class ie. You can only access and modify properties a and b.
person = Person()
person.a = 'a'
person.b = 'b'
person.c = 'c'. -> gives error
Requirements:
- To have attributes named in
__slots__to actually be stored in slots instead of a__dict__, a class must inherit from object. - To prevent the creation of a
__dict__, you must inherit from object and all classes in the inheritance must declare__slots__and none of them can have a__dict__entry.