@@ -1653,15 +1653,11 @@ instances cannot override the behavior of a property.
1653
1653
__slots__
1654
1654
^^^^^^^^^
1655
1655
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.)
1664
1659
1660
+ The space saved over using *__dict__ * can be significant.
1665
1661
1666
1662
.. data :: object.__slots__
1667
1663
@@ -1674,9 +1670,8 @@ saved because *__dict__* is not created for each instance.
1674
1670
Notes on using *__slots__ *
1675
1671
""""""""""""""""""""""""""
1676
1672
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.
1680
1675
1681
1676
* Without a *__dict__ * variable, instances cannot be assigned new variables not
1682
1677
listed in the *__slots__ * definition. Attempts to assign to an unlisted
@@ -1695,9 +1690,11 @@ Notes on using *__slots__*
1695
1690
*__slots__ *; otherwise, the class attribute would overwrite the descriptor
1696
1691
assignment.
1697
1692
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).
1701
1698
1702
1699
* If a class defines a slot also defined in a base class, the instance variable
1703
1700
defined by the base class slot is inaccessible (except by retrieving its
@@ -1713,6 +1710,10 @@ Notes on using *__slots__*
1713
1710
1714
1711
* *__class__ * assignment works only if both classes have the same *__slots__ *.
1715
1712
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 `.
1716
1717
1717
1718
.. _class-customization :
1718
1719
0 commit comments