Skip to content

Commit 6eab93c

Browse files
miss-islingtonaaronchall
authored andcommitted
bpo-30449: Improve __slots__ documentation (pythonGH-1819)
(cherry picked from commit 2b44e30) Co-authored-by: Aaron Hall, MBA <[email protected]>
1 parent d8e7b98 commit 6eab93c

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Doc/reference/datamodel.rst

+15-14
Original file line numberDiff line numberDiff line change
@@ -1653,15 +1653,11 @@ instances cannot override the behavior of a property.
16531653
__slots__
16541654
^^^^^^^^^
16551655

1656-
By default, instances of classes have a dictionary for attribute storage. This
1657-
wastes space for objects having very few instance variables. The space
1658-
consumption can become acute when creating large numbers of instances.
1659-
1660-
The default can be overridden by defining *__slots__* in a class definition.
1661-
The *__slots__* declaration takes a sequence of instance variables and reserves
1662-
just enough space in each instance to hold a value for each variable. Space is
1663-
saved because *__dict__* is not created for each instance.
1656+
*__slots__* allow us to explicitly declare data members (like
1657+
properties) and deny the creation of *__dict__* and *__weakref__*
1658+
(unless explicitly declared in *__slots__* or available in a parent.)
16641659

1660+
The space saved over using *__dict__* can be significant.
16651661

16661662
.. data:: object.__slots__
16671663

@@ -1674,9 +1670,8 @@ saved because *__dict__* is not created for each instance.
16741670
Notes on using *__slots__*
16751671
""""""""""""""""""""""""""
16761672

1677-
* When inheriting from a class without *__slots__*, the *__dict__* attribute of
1678-
that class will always be accessible, so a *__slots__* definition in the
1679-
subclass is meaningless.
1673+
* When inheriting from a class without *__slots__*, the *__dict__* and
1674+
*__weakref__* attribute of the instances will always be accessible.
16801675

16811676
* Without a *__dict__* variable, instances cannot be assigned new variables not
16821677
listed in the *__slots__* definition. Attempts to assign to an unlisted
@@ -1695,9 +1690,11 @@ Notes on using *__slots__*
16951690
*__slots__*; otherwise, the class attribute would overwrite the descriptor
16961691
assignment.
16971692

1698-
* The action of a *__slots__* declaration is limited to the class where it is
1699-
defined. As a result, subclasses will have a *__dict__* unless they also define
1700-
*__slots__* (which must only contain names of any *additional* slots).
1693+
* The action of a *__slots__* declaration is not limited to the class
1694+
where it is defined. *__slots__* declared in parents are available in
1695+
child classes. However, child subclasses will get a *__dict__* and
1696+
*__weakref__* unless they also define *__slots__* (which should only
1697+
contain names of any *additional* slots).
17011698

17021699
* If a class defines a slot also defined in a base class, the instance variable
17031700
defined by the base class slot is inaccessible (except by retrieving its
@@ -1713,6 +1710,10 @@ Notes on using *__slots__*
17131710

17141711
* *__class__* assignment works only if both classes have the same *__slots__*.
17151712

1713+
* Multiple inheritance with multiple slotted parent classes can be used,
1714+
but only one parent is allowed to have attributes created by slots
1715+
(the other bases must have empty slot layouts) - violations raise
1716+
:exc:`TypeError`.
17161717

17171718
.. _class-customization:
17181719

0 commit comments

Comments
 (0)