3.3.2.4 __slots__
By default, instances of both old and new-style classes have a dictionary
for attribute storage. This wastes space for objects having very few instance
variables. The space consumption can become acute when creating large numbers
of instances.
The default can be overridden by defining __slots__ in a new-style class
definition. The __slots__ declaration takes a sequence of instance
variables and reserves just enough space in each instance to hold a value
for each variable. Space is saved because __dict__ is not created for
each instance.
- __slots__
-
This class variable can be assigned a string, iterable, or sequence of strings
with variable names used by instances. If defined in a new-style class,
__slots__ reserves space for the declared variables
and prevents the automatic creation of __dict__ and __weakref__
for each instance.
New in version 2.2.
Notes on using __slots__
- Without a __dict__ variable, instances cannot be assigned new
variables not listed in the __slots__ definition. Attempts to assign
to an unlisted variable name raises AttributeError. If dynamic
assignment of new variables is desired, then add
'__dict__'
to the
sequence of strings in the __slots__ declaration.
Changed in version 2.3:
Previously, adding '__dict__'
to the __slots__
declaration would not enable the assignment of new attributes not
specifically listed in the sequence of instance variable names.
- Without a __weakref__ variable for each instance, classes
defining __slots__ do not support weak references to its instances.
If weak reference support is needed, then add
'__weakref__'
to the
sequence of strings in the __slots__ declaration.
Changed in version 2.3:
Previously, adding '__weakref__'
to the __slots__
declaration would not enable support for weak references.
- __slots__ are implemented at the class level by creating
descriptors (3.3.2) for each variable name. As a result,
class attributes cannot be used to set default values for instance
variables defined by __slots__; otherwise, the class attribute would
overwrite the descriptor assignment.
- If a class defines a slot also defined in a base class, the instance
variable defined by the base class slot is inaccessible (except by retrieving
its descriptor directly from the base class). This renders the meaning of the
program undefined. In the future, a check may be added to prevent this.
- The action of a __slots__ declaration is limited to the class
where it is defined. As a result, subclasses will have a __dict__
unless they also define __slots__.
- __slots__ do not work for classes derived from ``variable-length''
built-in types such as long, str and tuple.
- Any non-string iterable may be assigned to __slots__.
Mappings may also be used; however, in the future, special meaning may
be assigned to the values corresponding to each key.
See About this document... for information on suggesting changes.