Question Details

No question body available.

Tags

python python-typing slots

Answers (1)

January 4, 2026 Score: 0 Rep: 4,582 Quality: Low Completeness: 80%

@STerliakov's comment is the answer:

Also there's a well-known attrs package that supports slots=True (attrs.org/en/stable/api.html) - if you don't mind pulling an extra dep, having something dataclass-like consistent across many python versions could be better than writing your own backports for everything.

# pip install attrs
from typing import ClassVar
from attrs import define

@define(slots=True, weakrefslot=False, eq=True, frozen=True, cachehash=True) class C: foo: int bar: int nota_slot: ClassVar[str] = 'not a slot'

Testing this by filling 800,000 instances with randomness and inserting them into a dict, the memory and runtime performance of the @attrs.defined class is nearly identical to manually writing out slots (and a constructor, and hash and eq and so on).