Python Reference ยท Classes and Special Methods
__slots__
Reference for Python __slots__: fixed attribute names, saving memory per instance, no __dict__, inheritance rules, weakref and dataclass slots=True.
A class attribute named __slots__ tells Python exactly which instance attributes the class has. Instances then store them in a compact internal layout instead of a per-instance __dict__.
You get two effects: instances use less memory (useful with millions of small objects) and typos like p.colour = 1 raise AttributeError instead of silently creating a new attribute. The price is less flexibility.
class Point: __slots__ = ("x", "y") def __init__(self, x, y): self.x, self.y = x, y
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
class C: __slots__ = ("name1", "name2") # tuple of names __slots__ = "name" # a single string __slots__ = () # no instance attributes at all
| Value | Description |
|---|---|
| Iterable of strings | Names of the allowed instance attributes. Each becomes a descriptor on the class. |
"__dict__" in the list |
Brings back a normal __dict__ for extra attributes. |
"__weakref__" in the list |
Allows weak references to instances. |
Raises: AttributeError when assigning an attribute that is not in the slots (or reading a slot that has not been set). ValueError if a slot name conflicts with a class attribute defined in the same class body.
Usage notes
- No
__dict__, no__weakref__unless you list them. Slotted instances cannot be weakly referenced by default andvars(obj)fails. - Unset slots raise
AttributeErroron read, just like a missing attribute. Initialize every slot in__init__(). - Class-level defaults conflict. You cannot give a slot name a default value with
x = 5in the same class body; assign in__init__()instead. - Inheritance. A subclass without its own
__slots__gets a__dict__again, which cancels the saving. Each subclass should declare only its new names; repeating a parent's slot wastes memory. - Multiple inheritance works only if at most one base class has a non-empty slot layout.
- Not a security feature. It is a layout and memory optimization. Measure before using it; for a few objects the gain is negligible.
- Pickling works with protocol 2 and above; very old protocols need
__getstate__(). - Dataclasses.
@dataclass(slots=True)(Python 3.10+) builds the slots for you and returns a new class, sosuper()without arguments inside methods behaves correctly in modern versions. - Setting a slot is a descriptor call, so
Point.xon the class gives a member descriptor, not a value.