Skip to content

Commit 7da9025

Browse files
authored
bpo-45840: Improve cross-references in the data model documentation (GH-29633) (GH-30077)
(cherry picked from commit c0521fe)
1 parent 94483f1 commit 7da9025

File tree

2 files changed

+77
-54
lines changed

2 files changed

+77
-54
lines changed

Doc/reference/datamodel.rst

Lines changed: 76 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Ellipsis
188188
representation in computers.
189189

190190
The string representations of the numeric classes, computed by
191-
:meth:`__repr__` and :meth:`__str__`, have the following
191+
:meth:`~object.__repr__` and :meth:`~object.__str__`, have the following
192192
properties:
193193

194194
* They are valid numeric literals which, when passed to their
@@ -677,7 +677,8 @@ Callable types
677677
returns an :term:`asynchronous iterator` object which can be used in an
678678
:keyword:`async for` statement to execute the body of the function.
679679

680-
Calling the asynchronous iterator's :meth:`aiterator.__anext__` method
680+
Calling the asynchronous iterator's
681+
:meth:`aiterator.__anext__ <object.__anext__>` method
681682
will return an :term:`awaitable` which when awaited
682683
will execute until it provides a value using the :keyword:`yield`
683684
expression. When the function executes an empty :keyword:`return`
@@ -715,13 +716,13 @@ Callable types
715716
Classes
716717
Classes are callable. These objects normally act as factories for new
717718
instances of themselves, but variations are possible for class types that
718-
override :meth:`__new__`. The arguments of the call are passed to
719-
:meth:`__new__` and, in the typical case, to :meth:`__init__` to
719+
override :meth:`~object.__new__`. The arguments of the call are passed to
720+
:meth:`__new__` and, in the typical case, to :meth:`~object.__init__` to
720721
initialize the new instance.
721722

722723
Class Instances
723724
Instances of arbitrary classes can be made callable by defining a
724-
:meth:`__call__` method in their class.
725+
:meth:`~object.__call__` method in their class.
725726

726727

727728
Modules
@@ -880,14 +881,14 @@ Class instances
880881
section :ref:`descriptors` for another way in which attributes of a class
881882
retrieved via its instances may differ from the objects actually stored in
882883
the class's :attr:`~object.__dict__`. If no class attribute is found, and the
883-
object's class has a :meth:`__getattr__` method, that is called to satisfy
884+
object's class has a :meth:`~object.__getattr__` method, that is called to satisfy
884885
the lookup.
885886

886887
.. index:: triple: class instance; attribute; assignment
887888

888889
Attribute assignments and deletions update the instance's dictionary, never a
889-
class's dictionary. If the class has a :meth:`__setattr__` or
890-
:meth:`__delattr__` method, this is called instead of updating the instance
890+
class's dictionary. If the class has a :meth:`~object.__setattr__` or
891+
:meth:`~object.__delattr__` method, this is called instead of updating the instance
891892
dictionary directly.
892893

893894
.. index::
@@ -1141,7 +1142,8 @@ Internal types
11411142
Slice objects
11421143
.. index:: builtin: slice
11431144

1144-
Slice objects are used to represent slices for :meth:`__getitem__`
1145+
Slice objects are used to represent slices for
1146+
:meth:`~object.__getitem__`
11451147
methods. They are also created by the built-in :func:`slice` function.
11461148

11471149
.. index::
@@ -1194,17 +1196,18 @@ A class can implement certain operations that are invoked by special syntax
11941196
(such as arithmetic operations or subscripting and slicing) by defining methods
11951197
with special names. This is Python's approach to :dfn:`operator overloading`,
11961198
allowing classes to define their own behavior with respect to language
1197-
operators. For instance, if a class defines a method named :meth:`__getitem__`,
1199+
operators. For instance, if a class defines a method named
1200+
:meth:`~object.__getitem__`,
11981201
and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent
11991202
to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute an
12001203
operation raise an exception when no appropriate method is defined (typically
12011204
:exc:`AttributeError` or :exc:`TypeError`).
12021205

12031206
Setting a special method to ``None`` indicates that the corresponding
12041207
operation is not available. For example, if a class sets
1205-
:meth:`__iter__` to ``None``, the class is not iterable, so calling
1208+
:meth:`~object.__iter__` to ``None``, the class is not iterable, so calling
12061209
:func:`iter` on its instances will raise a :exc:`TypeError` (without
1207-
falling back to :meth:`__getitem__`). [#]_
1210+
falling back to :meth:`~object.__getitem__`). [#]_
12081211

12091212
When implementing a class that emulates any built-in type, it is important that
12101213
the emulation only be implemented to the degree that it makes sense for the
@@ -1754,7 +1757,8 @@ Invoking Descriptors
17541757

17551758
In general, a descriptor is an object attribute with "binding behavior", one
17561759
whose attribute access has been overridden by methods in the descriptor
1757-
protocol: :meth:`__get__`, :meth:`__set__`, and :meth:`__delete__`. If any of
1760+
protocol: :meth:`~object.__get__`, :meth:`~object.__set__`, and
1761+
:meth:`~object.__delete__`. If any of
17581762
those methods are defined for an object, it is said to be a descriptor.
17591763

17601764
The default behavior for attribute access is to get, set, or delete the
@@ -1790,7 +1794,8 @@ Super Binding
17901794

17911795
For instance bindings, the precedence of descriptor invocation depends on
17921796
which descriptor methods are defined. A descriptor can define any combination
1793-
of :meth:`__get__`, :meth:`__set__` and :meth:`__delete__`. If it does not
1797+
of :meth:`~object.__get__`, :meth:`~object.__set__` and
1798+
:meth:`~object.__delete__`. If it does not
17941799
define :meth:`__get__`, then accessing the attribute will return the descriptor
17951800
object itself unless there is a value in the object's instance dictionary. If
17961801
the descriptor defines :meth:`__set__` and/or :meth:`__delete__`, it is a data
@@ -1801,7 +1806,8 @@ descriptors have just the :meth:`__get__` method. Data descriptors with
18011806
instance dictionary. In contrast, non-data descriptors can be overridden by
18021807
instances.
18031808

1804-
Python methods (including :func:`staticmethod` and :func:`classmethod`) are
1809+
Python methods (including those decorated with
1810+
:func:`@staticmethod <staticmethod>` and :func:`@classmethod <classmethod>`) are
18051811
implemented as non-data descriptors. Accordingly, instances can redefine and
18061812
override methods. This allows individual instances to acquire behaviors that
18071813
differ from other instances of the same class.
@@ -1816,46 +1822,50 @@ __slots__
18161822
^^^^^^^^^
18171823

18181824
*__slots__* allow us to explicitly declare data members (like
1819-
properties) and deny the creation of *__dict__* and *__weakref__*
1825+
properties) and deny the creation of :attr:`~object.__dict__` and *__weakref__*
18201826
(unless explicitly declared in *__slots__* or available in a parent.)
18211827

1822-
The space saved over using *__dict__* can be significant.
1828+
The space saved over using :attr:`~object.__dict__` can be significant.
18231829
Attribute lookup speed can be significantly improved as well.
18241830

18251831
.. data:: object.__slots__
18261832

18271833
This class variable can be assigned a string, iterable, or sequence of
18281834
strings with variable names used by instances. *__slots__* reserves space
1829-
for the declared variables and prevents the automatic creation of *__dict__*
1835+
for the declared variables and prevents the automatic creation of
1836+
:attr:`~object.__dict__`
18301837
and *__weakref__* for each instance.
18311838

18321839

18331840
Notes on using *__slots__*
18341841
""""""""""""""""""""""""""
18351842

1836-
* When inheriting from a class without *__slots__*, the *__dict__* and
1843+
* When inheriting from a class without *__slots__*, the
1844+
:attr:`~object.__dict__` and
18371845
*__weakref__* attribute of the instances will always be accessible.
18381846

1839-
* Without a *__dict__* variable, instances cannot be assigned new variables not
1847+
* Without a :attr:`~object.__dict__` variable, instances cannot be assigned new
1848+
variables not
18401849
listed in the *__slots__* definition. Attempts to assign to an unlisted
18411850
variable name raises :exc:`AttributeError`. If dynamic assignment of new
18421851
variables is desired, then add ``'__dict__'`` to the sequence of strings in
18431852
the *__slots__* declaration.
18441853

18451854
* Without a *__weakref__* variable for each instance, classes defining
1846-
*__slots__* do not support weak references to its instances. If weak reference
1855+
*__slots__* do not support :mod:`weak references <weakref>` to its instances.
1856+
If weak reference
18471857
support is needed, then add ``'__weakref__'`` to the sequence of strings in the
18481858
*__slots__* declaration.
18491859

1850-
* *__slots__* are implemented at the class level by creating descriptors
1851-
(:ref:`descriptors`) for each variable name. As a result, class attributes
1860+
* *__slots__* are implemented at the class level by creating :ref:`descriptors <descriptors>`
1861+
for each variable name. As a result, class attributes
18521862
cannot be used to set default values for instance variables defined by
18531863
*__slots__*; otherwise, the class attribute would overwrite the descriptor
18541864
assignment.
18551865

18561866
* The action of a *__slots__* declaration is not limited to the class
18571867
where it is defined. *__slots__* declared in parents are available in
1858-
child classes. However, child subclasses will get a *__dict__* and
1868+
child classes. However, child subclasses will get a :attr:`~object.__dict__` and
18591869
*__weakref__* unless they also define *__slots__* (which should only
18601870
contain names of any *additional* slots).
18611871

@@ -1871,14 +1881,17 @@ Notes on using *__slots__*
18711881
used; however, in the future, special meaning may be assigned to the values
18721882
corresponding to each key.
18731883

1874-
* *__class__* assignment works only if both classes have the same *__slots__*.
1884+
* :attr:`~instance.__class__` assignment works only if both classes have the
1885+
same *__slots__*.
18751886

1876-
* Multiple inheritance with multiple slotted parent classes can be used,
1887+
* :ref:`Multiple inheritance <tut-multiple>` with multiple slotted parent
1888+
classes can be used,
18771889
but only one parent is allowed to have attributes created by slots
18781890
(the other bases must have empty slot layouts) - violations raise
18791891
:exc:`TypeError`.
18801892

1881-
* If an iterator is used for *__slots__* then a descriptor is created for each
1893+
* If an :term:`iterator` is used for *__slots__* then a :term:`descriptor` is
1894+
created for each
18821895
of the iterator's values. However, the *__slots__* attribute will be an empty
18831896
iterator.
18841897

@@ -1887,7 +1900,7 @@ Notes on using *__slots__*
18871900
Customizing class creation
18881901
--------------------------
18891902

1890-
Whenever a class inherits from another class, *__init_subclass__* is
1903+
Whenever a class inherits from another class, :meth:`~object.__init_subclass__` is
18911904
called on that class. This way, it is possible to write classes which
18921905
change the behavior of subclasses. This is closely related to class
18931906
decorators, but where class decorators only affect the specific class they're
@@ -1928,7 +1941,7 @@ class defining the method.
19281941

19291942

19301943
When a class is created, :meth:`type.__new__` scans the class variables
1931-
and makes callbacks to those with a :meth:`__set_name__` hook.
1944+
and makes callbacks to those with a :meth:`~object.__set_name__` hook.
19321945

19331946
.. method:: object.__set_name__(self, owner, name)
19341947

@@ -2040,7 +2053,8 @@ Once the appropriate metaclass has been identified, then the class namespace
20402053
is prepared. If the metaclass has a ``__prepare__`` attribute, it is called
20412054
as ``namespace = metaclass.__prepare__(name, bases, **kwds)`` (where the
20422055
additional keyword arguments, if any, come from the class definition). The
2043-
``__prepare__`` method should be implemented as a :func:`classmethod`. The
2056+
``__prepare__`` method should be implemented as a
2057+
:func:`classmethod <classmethod>`. The
20442058
namespace returned by ``__prepare__`` is passed in to ``__new__``, but when
20452059
the final class object is created the namespace is copied into a new ``dict``.
20462060

@@ -2338,31 +2352,36 @@ Emulating container types
23382352
-------------------------
23392353

23402354
The following methods can be defined to implement container objects. Containers
2341-
usually are sequences (such as lists or tuples) or mappings (like dictionaries),
2355+
usually are :term:`sequences <sequence>` (such as :class:`lists <list>` or
2356+
:class:`tuples <tuple>`) or :term:`mappings <mapping>` (like
2357+
:class:`dictionaries <dict>`),
23422358
but can represent other containers as well. The first set of methods is used
23432359
either to emulate a sequence or to emulate a mapping; the difference is that for
23442360
a sequence, the allowable keys should be the integers *k* for which ``0 <= k <
2345-
N`` where *N* is the length of the sequence, or slice objects, which define a
2361+
N`` where *N* is the length of the sequence, or :class:`slice` objects, which define a
23462362
range of items. It is also recommended that mappings provide the methods
23472363
:meth:`keys`, :meth:`values`, :meth:`items`, :meth:`get`, :meth:`clear`,
23482364
:meth:`setdefault`, :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and
2349-
:meth:`update` behaving similar to those for Python's standard dictionary
2365+
:meth:`update` behaving similar to those for Python's standard :class:`dictionary <dict>`
23502366
objects. The :mod:`collections.abc` module provides a
23512367
:class:`~collections.abc.MutableMapping`
2352-
abstract base class to help create those methods from a base set of
2353-
:meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`, and :meth:`keys`.
2368+
:term:`abstract base class` to help create those methods from a base set of
2369+
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`, :meth:`~object.__delitem__`, and :meth:`keys`.
23542370
Mutable sequences should provide methods :meth:`append`, :meth:`count`,
23552371
:meth:`index`, :meth:`extend`, :meth:`insert`, :meth:`pop`, :meth:`remove`,
2356-
:meth:`reverse` and :meth:`sort`, like Python standard list objects. Finally,
2372+
:meth:`reverse` and :meth:`sort`, like Python standard :class:`list`
2373+
objects. Finally,
23572374
sequence types should implement addition (meaning concatenation) and
2358-
multiplication (meaning repetition) by defining the methods :meth:`__add__`,
2359-
:meth:`__radd__`, :meth:`__iadd__`, :meth:`__mul__`, :meth:`__rmul__` and
2360-
:meth:`__imul__` described below; they should not define other numerical
2375+
multiplication (meaning repetition) by defining the methods
2376+
:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`,
2377+
:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__`
2378+
described below; they should not define other numerical
23612379
operators. It is recommended that both mappings and sequences implement the
2362-
:meth:`__contains__` method to allow efficient use of the ``in`` operator; for
2380+
:meth:`~object.__contains__` method to allow efficient use of the ``in``
2381+
operator; for
23632382
mappings, ``in`` should search the mapping's keys; for sequences, it should
23642383
search through the values. It is further recommended that both mappings and
2365-
sequences implement the :meth:`__iter__` method to allow efficient iteration
2384+
sequences implement the :meth:`~object.__iter__` method to allow efficient iteration
23662385
through the container; for mappings, :meth:`__iter__` should iterate
23672386
through the object's keys; for sequences, it should iterate through the values.
23682387

@@ -2774,7 +2793,8 @@ exception::
27742793
TypeError: object of type 'C' has no len()
27752794

27762795
The rationale behind this behaviour lies with a number of special methods such
2777-
as :meth:`__hash__` and :meth:`__repr__` that are implemented by all objects,
2796+
as :meth:`~object.__hash__` and :meth:`~object.__repr__` that are implemented
2797+
by all objects,
27782798
including type objects. If the implicit lookup of these methods used the
27792799
conventional lookup process, they would fail when invoked on the type object
27802800
itself::
@@ -2797,7 +2817,7 @@ the instance when looking up special methods::
27972817

27982818
In addition to bypassing any instance attributes in the interest of
27992819
correctness, implicit special method lookup generally also bypasses the
2800-
:meth:`__getattribute__` method even of the object's metaclass::
2820+
:meth:`~object.__getattribute__` method even of the object's metaclass::
28012821

28022822
>>> class Meta(type):
28032823
... def __getattribute__(*args):
@@ -2821,7 +2841,7 @@ correctness, implicit special method lookup generally also bypasses the
28212841
>>> len(c) # Implicit lookup
28222842
10
28232843

2824-
Bypassing the :meth:`__getattribute__` machinery in this fashion
2844+
Bypassing the :meth:`~object.__getattribute__` machinery in this fashion
28252845
provides significant scope for speed optimisations within the
28262846
interpreter, at the cost of some flexibility in the handling of
28272847
special methods (the special method *must* be set on the class
@@ -2838,15 +2858,15 @@ Coroutines
28382858
Awaitable Objects
28392859
-----------------
28402860

2841-
An :term:`awaitable` object generally implements an :meth:`__await__` method.
2861+
An :term:`awaitable` object generally implements an :meth:`~object.__await__` method.
28422862
:term:`Coroutine objects <coroutine>` returned from :keyword:`async def` functions
28432863
are awaitable.
28442864

28452865
.. note::
28462866

28472867
The :term:`generator iterator` objects returned from generators
28482868
decorated with :func:`types.coroutine` or :func:`asyncio.coroutine`
2849-
are also awaitable, but they do not implement :meth:`__await__`.
2869+
are also awaitable, but they do not implement :meth:`~object.__await__`.
28502870

28512871
.. method:: object.__await__(self)
28522872

@@ -2865,7 +2885,7 @@ Coroutine Objects
28652885
-----------------
28662886

28672887
:term:`Coroutine objects <coroutine>` are :term:`awaitable` objects.
2868-
A coroutine's execution can be controlled by calling :meth:`__await__` and
2888+
A coroutine's execution can be controlled by calling :meth:`~object.__await__` and
28692889
iterating over the result. When the coroutine has finished executing and
28702890
returns, the iterator raises :exc:`StopIteration`, and the exception's
28712891
:attr:`~StopIteration.value` attribute holds the return value. If the
@@ -2884,7 +2904,7 @@ generators, coroutines do not directly support iteration.
28842904

28852905
Starts or resumes execution of the coroutine. If *value* is ``None``,
28862906
this is equivalent to advancing the iterator returned by
2887-
:meth:`__await__`. If *value* is not ``None``, this method delegates
2907+
:meth:`~object.__await__`. If *value* is not ``None``, this method delegates
28882908
to the :meth:`~generator.send` method of the iterator that caused
28892909
the coroutine to suspend. The result (return value,
28902910
:exc:`StopIteration`, or other exception) is the same as when
@@ -2897,7 +2917,7 @@ generators, coroutines do not directly support iteration.
28972917
the coroutine to suspend, if it has such a method. Otherwise,
28982918
the exception is raised at the suspension point. The result
28992919
(return value, :exc:`StopIteration`, or other exception) is the same as
2900-
when iterating over the :meth:`__await__` return value, described
2920+
when iterating over the :meth:`~object.__await__` return value, described
29012921
above. If the exception is not caught in the coroutine, it propagates
29022922
back to the caller.
29032923

@@ -2951,11 +2971,11 @@ An example of an asynchronous iterable object::
29512971
.. versionadded:: 3.5
29522972

29532973
.. versionchanged:: 3.7
2954-
Prior to Python 3.7, ``__aiter__`` could return an *awaitable*
2974+
Prior to Python 3.7, :meth:`~object.__aiter__` could return an *awaitable*
29552975
that would resolve to an
29562976
:term:`asynchronous iterator <asynchronous iterator>`.
29572977

2958-
Starting with Python 3.7, ``__aiter__`` must return an
2978+
Starting with Python 3.7, :meth:`~object.__aiter__` must return an
29592979
asynchronous iterator object. Returning anything else
29602980
will result in a :exc:`TypeError` error.
29612981

@@ -2998,8 +3018,9 @@ An example of an asynchronous context manager class::
29983018
controlled conditions. It generally isn't a good idea though, since it can
29993019
lead to some very strange behaviour if it is handled incorrectly.
30003020
3001-
.. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
3002-
:meth:`__contains__` methods have special handling for this; others
3021+
.. [#] The :meth:`~object.__hash__`, :meth:`~object.__iter__`,
3022+
:meth:`~object.__reversed__`, and :meth:`~object.__contains__` methods have
3023+
special handling for this; others
30033024
will still raise a :exc:`TypeError`, but may do so by relying on
30043025
the behavior that ``None`` is not callable.
30053026
@@ -3010,5 +3031,6 @@ An example of an asynchronous context manager class::
30103031
*blocking* such fallback.
30113032
30123033
.. [#] For operands of the same type, it is assumed that if the non-reflected
3013-
method -- such as :meth:`__add__` -- fails then the overall operation is not
3034+
method -- such as :meth:`~object.__add__` -- fails then the overall
3035+
operation is not
30143036
supported, which is why the reflected method is not called.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve cross-references in the documentation for the data model.

0 commit comments

Comments
 (0)