slots
How can dataclasses be made to work better with __slots__?
2021 UPDATE: direct support for __slots__ is added to python 3.10. I am leaving this answer for posterity and won’t be updating it. The problem is not unique to dataclasses. ANY conflicting class attribute will stomp all over a slot: >>> class Failure: … __slots__ = tuple(“xyz”) … x=1 … Traceback (most recent call last): … Read more
How does inheritance of __slots__ in subclasses actually work?
As others have mentioned, the sole reason for defining __slots__ is to save some memory, when you have simple objects with a predefined set of attributes and don’t want each to carry around a dictionary. This is meaningful only for classes of which you plan to have many instances, of course. The savings may not … Read more
Usage of __slots__?
In Python, what is the purpose of __slots__ and what are the cases one should avoid this? TLDR: The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have, with the expected results: faster attribute access. space savings in memory. The space savings is from Storing value … Read more