Documentation on __slots__ is confusing: is it a class or object variable?

On “3. Data model - object._slots_ — Python 3.14.3 documentation”, I read

This class variable can be assigned a string, iterable, or sequence of strings with variable names used by instances. _slots_ reserves space for the declared variables and prevents the automatic creation of __dict__ and _weakref_ for each instance.

Why is it documented as object.__slots__ while described as class.__slots__?

Object and class variables are not always the same, as is e.g., the case for __dict__:

class A: 
   pass

a = A()

print(A.__dict__ == a.__dict__)

This code prints False.

I hope my feedback helps to improve the documentation even further!

object is the base class of all objects. __slots__ is a class variable that is valid on any subclass of object.

I am not sure what the confusion here is. It is clearly document as belonging to the class.

4 Likes

By the time an instance of any object is created and instance variables can be assigned on it, it’s too late to create any __slots__ and rearrange how the other instance variables’re stored in memory.

FWIW the optimisation of __dict__and classes in Python is that good now, that I’ve found little speed increase from using slots these days.