review-slots-and-memory
Treat slots as a targeted memory optimization, recommended only for classes
SKILL.mdsubagents/python-reviewer/skills/review-slots-and-memory/SKILL.md
GitHubReview slots and memory
Purpose
Treat __slots__ as a targeted memory optimization, recommended only for classes
instantiated in very large numbers with a fixed attribute set, and always reported with
its trade-offs — not as a default. (P12)
When to use
- A class is instantiated in very large numbers with a fixed, known set of attributes, and memory use is a real concern.
Do not recommend __slots__ when instance counts are small, the attribute set is dynamic,
or the class relies on inheritance or on the instance __dict__.
Procedure
- Confirm the qualifying conditions. Recommend
__slots__only when both hold: the class is instantiated in large numbers, and its attribute set is fixed and known. By default an instance stores attributes in a per-instance__dict__;__slots__replaces that dict with a compact array, which can cut memory substantially at scale. (C013) - State the trade-offs alongside the benefit. Always report that
__slots__interacts badly with inheritance (especially multiple inheritance) and removes the instance__dict__, so dynamic attribute assignment and code that relies on__dict__stop working. Present it as a targeted optimization, not a reflex. - Do not recommend it outside scope. For small instance counts, dynamic attributes, or
classes built on inheritance /
__dict__, advise against__slots__. - Keep memory in scope, not runtime speed. This is a memory optimization; it is not a substitute for runtime/algorithmic performance work, which is out of this reviewer's scope.
Inputs
- The class under review, its approximate instance count at runtime, whether its attribute
set is fixed, and whether it participates in inheritance or relies on
__dict__.
Output
An advise/review finding: whether __slots__ is warranted here, the memory benefit,
and the inheritance/__dict__ trade-offs stated alongside — never a bare "add __slots__".
Traced to P12.
References
- pythonic-review-checklist — the
__slots__row.
Provenance
Derived from principle P12 (claim C013, evidence E013; medium confidence),
grounded in Python Distilled's discussion of __slots__ and its trade-offs (anchor
python-distilled-pea-1baf485f-h0260). Distillation-only source: paraphrased, no verbatim
quotation.