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

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

tech