Sub topic 4
How slots work: member descriptors on the class
This chapter assumes you know how __slots__ is declared and what it prevents: new attributes and the instance __dict__. It also assumes you know what a class attribute is. Here you will see the mechanism behind that behavior. Slots are implemented with descriptors stored on the class.
What `__slots__` creates
When Python builds a class that declares __slots__, it does two things:
- It reserves a fixed amount of space inside each instance, one pointer per slot name.
- It adds one object per slot name to the class namespace. Each of these objects is a member descriptor.
A descriptor is an object stored on a class that defines __get__, and optionally __set__ and __delete__. Python calls these methods when you read, assign, or delete the attribute of the same name on an instance. Properties and methods work the same way.
You can see the member descriptors by looking in the class __dict__:
class Point:
__slots__ = ("x", "y")
descriptor = Point.__dict__["x"]
print(type(descriptor))
print(Point.x)
p = Point()
try:
p.x
except AttributeError:
print("slot x is not set yet")
p.x = 10
print(p.x)The first print shows <class 'member_descriptor'>. Reading Point.x on the class returns the descriptor itself, shown as <member 'x' of 'Point' objects>. No value is stored on the class. The value lives in the instance's reserved space.
Calling the descriptor by hand
The descriptor methods are ordinary methods, so you can call them directly. This shows what happens when you write p.x.
class Point:
__slots__ = ("x", "y")
descriptor = Point.__dict__["x"]
p = Point()
descriptor.__set__(p, 20)
print(descriptor.__get__(p, Point))
print(p.x)
descriptor.__delete__(p)
print(hasattr(p, "x"))This prints 20, 20, and False. The three operations map onto the three attribute actions:
| You write | Python calls | Effect on the slot |
|---|---|---|
| `p.x` | `descriptor.__get__(p, Point)` | Returns the stored value, or raises `AttributeError` if empty |
| `p.x = 5` | `descriptor.__set__(p, 5)` | Stores the value in the instance's reserved space |
| `del p.x` | `descriptor.__delete__(p)` | Empties the slot |
A slot can be empty. A new instance starts with every slot empty, which is why reading p.x before assigning it raises AttributeError. A deleted slot goes back to that same state, and you can assign to it again later.
Why the descriptor wins the lookup
A member descriptor defines __set__, so it is a data descriptor. During attribute lookup, Python checks the class first for data descriptors. If it finds one, it uses it and never consults an instance __dict__.
This is why the slot name keeps working even if you later opt back into __dict__ (a later chapter covers that). The slot still takes priority over any instance dictionary entry with the same name.
Contrast with the default storage
The table below compares a regular attribute and a slot.
| Row | Regular attribute | Slot attribute |
|---|---|---|
| Where the value lives | Entry in `p.__dict__` | Fixed position inside the instance |
| What the class holds | Nothing for that name (unless you define a class attribute) | A member descriptor |
| Who handles the read | Generic lookup in `__dict__` | The descriptor's `__get__` |
| New names at runtime | Allowed | Not allowed |
The descriptor is also what makes the restriction work. A name without a descriptor and without a __dict__ has nowhere to go, so assigning it raises AttributeError.
Common mistakes
- Expecting
Point.xto return a value. On the class it returns the descriptor. Read the value from an instance. - Assigning a class attribute with the same name as a slot. This replaces the member descriptor in the class namespace, so Python raises
ValueErrorwhen it creates the class. The next chapter covers this in detail. - Treating an unset slot as
None. It is empty, and reading it raisesAttributeError. Usehasattror set every slot in__init__.
Exercise
Given class Point: __slots__ = ("x", "y") and p = Point(), what does Point.__dict__["x"].__get__(p, Point) do before any assignment, and why?
Show answer
It raises AttributeError. The member descriptor reads the instance's slot for x, and that slot is empty because nothing has been assigned yet. A slot starts empty rather than holding None, so the read fails exactly as p.x would.