Skip to content
Tutorials Center
My learning

Topics

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.

Python
class Point:
    __slots__ = ("x", "y")
    def __init__(self, x, y):
        self.x, self.y = x, y

Try it

Python Slots restrict attributes

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.

Python Inheritance, defaults and dataclasses

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

Python
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 and vars(obj) fails.
  • Unset slots raise AttributeError on 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 = 5 in 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, so super() without arguments inside methods behaves correctly in modern versions.
  • Setting a slot is a descriptor call, so Point.x on the class gives a member descriptor, not a value.