Skip to content

Commit 2b44e30

Browse files
aaronchallrhettinger
authored andcommitted
bpo-30449 Terse slots (python#1819)
* correct __slots__ documentation with minimal changes * add multiple inheritance info * remove mapping from description
1 parent 368cf1d commit 2b44e30

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Doc/reference/datamodel.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,15 +1614,11 @@ instances cannot override the behavior of a property.
16141614
__slots__
16151615
^^^^^^^^^
16161616

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

1621+
The space saved over using *__dict__* can be significant.
16261622

16271623
.. data:: object.__slots__
16281624

@@ -1635,9 +1631,8 @@ saved because *__dict__* is not created for each instance.
16351631
Notes on using *__slots__*
16361632
""""""""""""""""""""""""""
16371633

1638-
* When inheriting from a class without *__slots__*, the *__dict__* attribute of
1639-
that class will always be accessible, so a *__slots__* definition in the
1640-
subclass is meaningless.
1634+
* When inheriting from a class without *__slots__*, the *__dict__* and
1635+
*__weakref__* attribute of the instances will always be accessible.
16411636

16421637
* Without a *__dict__* variable, instances cannot be assigned new variables not
16431638
listed in the *__slots__* definition. Attempts to assign to an unlisted
@@ -1656,9 +1651,11 @@ Notes on using *__slots__*
16561651
*__slots__*; otherwise, the class attribute would overwrite the descriptor
16571652
assignment.
16581653

1659-
* The action of a *__slots__* declaration is limited to the class where it is
1660-
defined. As a result, subclasses will have a *__dict__* unless they also define
1661-
*__slots__* (which must only contain names of any *additional* slots).
1654+
* The action of a *__slots__* declaration is not limited to the class
1655+
where it is defined. *__slots__* declared in parents are available in
1656+
child classes. However, child subclasses will get a *__dict__* and
1657+
*__weakref__* unless they also define *__slots__* (which should only
1658+
contain names of any *additional* slots).
16621659

16631660
* If a class defines a slot also defined in a base class, the instance variable
16641661
defined by the base class slot is inaccessible (except by retrieving its
@@ -1674,6 +1671,10 @@ Notes on using *__slots__*
16741671

16751672
* *__class__* assignment works only if both classes have the same *__slots__*.
16761673

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

16781679
.. _class-customization:
16791680

0 commit comments

Comments
 (0)