bpo-30449: Improve slots documentation (GH-1819) · python/cpython@6eab93c (original) (raw)

`@@ -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

``

`-

`

1678

``

`-

that class will always be accessible, so a slots definition in the

`

1679

``

`-

subclass is meaningless.

`

``

1673

`+

`

``

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

``

`-

`

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

`+

`

``

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

`+

`

``

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

``