Skip to content

Commit 2029c58

Browse files
authored
[3.9] bpo-45840: Improve cross-references in the data model documentation (GH-29633) (GH-30081)
Backport of GH-29633 to the 3.9 branch
1 parent 80eb8ab commit 2029c58

File tree

2 files changed

+76
-53
lines changed

2 files changed

+76
-53
lines changed

Doc/reference/datamodel.rst

+75-53
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
@@ -674,7 +674,8 @@ Callable types
674674
returns an asynchronous iterator object which can be used in an
675675
:keyword:`async for` statement to execute the body of the function.
676676

677-
Calling the asynchronous iterator's :meth:`aiterator.__anext__` method
677+
Calling the asynchronous iterator's
678+
:meth:`aiterator.__anext__ <object.__anext__>` method
678679
will return an :term:`awaitable` which when awaited
679680
will execute until it provides a value using the :keyword:`yield`
680681
expression. When the function executes an empty :keyword:`return`
@@ -712,13 +713,13 @@ Callable types
712713
Classes
713714
Classes are callable. These objects normally act as factories for new
714715
instances of themselves, but variations are possible for class types that
715-
override :meth:`__new__`. The arguments of the call are passed to
716-
:meth:`__new__` and, in the typical case, to :meth:`__init__` to
716+
override :meth:`~object.__new__`. The arguments of the call are passed to
717+
:meth:`__new__` and, in the typical case, to :meth:`~object.__init__` to
717718
initialize the new instance.
718719

719720
Class Instances
720721
Instances of arbitrary classes can be made callable by defining a
721-
:meth:`__call__` method in their class.
722+
:meth:`~object.__call__` method in their class.
722723

723724

724725
Modules
@@ -848,14 +849,14 @@ Class instances
848849
section :ref:`descriptors` for another way in which attributes of a class
849850
retrieved via its instances may differ from the objects actually stored in
850851
the class's :attr:`~object.__dict__`. If no class attribute is found, and the
851-
object's class has a :meth:`__getattr__` method, that is called to satisfy
852+
object's class has a :meth:`~object.__getattr__` method, that is called to satisfy
852853
the lookup.
853854

854855
.. index:: triple: class instance; attribute; assignment
855856

856857
Attribute assignments and deletions update the instance's dictionary, never a
857-
class's dictionary. If the class has a :meth:`__setattr__` or
858-
:meth:`__delattr__` method, this is called instead of updating the instance
858+
class's dictionary. If the class has a :meth:`~object.__setattr__` or
859+
:meth:`~object.__delattr__` method, this is called instead of updating the instance
859860
dictionary directly.
860861

861862
.. index::
@@ -1109,7 +1110,8 @@ Internal types
11091110
Slice objects
11101111
.. index:: builtin: slice
11111112

1112-
Slice objects are used to represent slices for :meth:`__getitem__`
1113+
Slice objects are used to represent slices for
1114+
:meth:`~object.__getitem__`
11131115
methods. They are also created by the built-in :func:`slice` function.
11141116

11151117
.. index::
@@ -1163,17 +1165,18 @@ A class can implement certain operations that are invoked by special syntax
11631165
(such as arithmetic operations or subscripting and slicing) by defining methods
11641166
with special names. This is Python's approach to :dfn:`operator overloading`,
11651167
allowing classes to define their own behavior with respect to language
1166-
operators. For instance, if a class defines a method named :meth:`__getitem__`,
1168+
operators. For instance, if a class defines a method named
1169+
:meth:`~object.__getitem__`,
11671170
and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent
11681171
to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute an
11691172
operation raise an exception when no appropriate method is defined (typically
11701173
:exc:`AttributeError` or :exc:`TypeError`).
11711174

11721175
Setting a special method to ``None`` indicates that the corresponding
11731176
operation is not available. For example, if a class sets
1174-
:meth:`__iter__` to ``None``, the class is not iterable, so calling
1177+
:meth:`~object.__iter__` to ``None``, the class is not iterable, so calling
11751178
:func:`iter` on its instances will raise a :exc:`TypeError` (without
1176-
falling back to :meth:`__getitem__`). [#]_
1179+
falling back to :meth:`~object.__getitem__`). [#]_
11771180

11781181
When implementing a class that emulates any built-in type, it is important that
11791182
the emulation only be implemented to the degree that it makes sense for the
@@ -1745,7 +1748,8 @@ Invoking Descriptors
17451748

17461749
In general, a descriptor is an object attribute with "binding behavior", one
17471750
whose attribute access has been overridden by methods in the descriptor
1748-
protocol: :meth:`__get__`, :meth:`__set__`, and :meth:`__delete__`. If any of
1751+
protocol: :meth:`~object.__get__`, :meth:`~object.__set__`, and
1752+
:meth:`~object.__delete__`. If any of
17491753
those methods are defined for an object, it is said to be a descriptor.
17501754

17511755
The default behavior for attribute access is to get, set, or delete the
@@ -1781,7 +1785,8 @@ Super Binding
17811785

17821786
For instance bindings, the precedence of descriptor invocation depends on
17831787
which descriptor methods are defined. A descriptor can define any combination
1784-
of :meth:`__get__`, :meth:`__set__` and :meth:`__delete__`. If it does not
1788+
of :meth:`~object.__get__`, :meth:`~object.__set__` and
1789+
:meth:`~object.__delete__`. If it does not
17851790
define :meth:`__get__`, then accessing the attribute will return the descriptor
17861791
object itself unless there is a value in the object's instance dictionary. If
17871792
the descriptor defines :meth:`__set__` and/or :meth:`__delete__`, it is a data
@@ -1792,7 +1797,8 @@ descriptors have just the :meth:`__get__` method. Data descriptors with
17921797
instance dictionary. In contrast, non-data descriptors can be overridden by
17931798
instances.
17941799

1795-
Python methods (including :func:`staticmethod` and :func:`classmethod`) are
1800+
Python methods (including those decorated with
1801+
:func:`@staticmethod <staticmethod>` and :func:`@classmethod <classmethod>`) are
17961802
implemented as non-data descriptors. Accordingly, instances can redefine and
17971803
override methods. This allows individual instances to acquire behaviors that
17981804
differ from other instances of the same class.
@@ -1807,46 +1813,50 @@ __slots__
18071813
^^^^^^^^^
18081814

18091815
*__slots__* allow us to explicitly declare data members (like
1810-
properties) and deny the creation of *__dict__* and *__weakref__*
1816+
properties) and deny the creation of :attr:`~object.__dict__` and *__weakref__*
18111817
(unless explicitly declared in *__slots__* or available in a parent.)
18121818

1813-
The space saved over using *__dict__* can be significant.
1819+
The space saved over using :attr:`~object.__dict__` can be significant.
18141820
Attribute lookup speed can be significantly improved as well.
18151821

18161822
.. data:: object.__slots__
18171823

18181824
This class variable can be assigned a string, iterable, or sequence of
18191825
strings with variable names used by instances. *__slots__* reserves space
1820-
for the declared variables and prevents the automatic creation of *__dict__*
1826+
for the declared variables and prevents the automatic creation of
1827+
:attr:`~object.__dict__`
18211828
and *__weakref__* for each instance.
18221829

18231830

18241831
Notes on using *__slots__*
18251832
""""""""""""""""""""""""""
18261833

1827-
* When inheriting from a class without *__slots__*, the *__dict__* and
1834+
* When inheriting from a class without *__slots__*, the
1835+
:attr:`~object.__dict__` and
18281836
*__weakref__* attribute of the instances will always be accessible.
18291837

1830-
* Without a *__dict__* variable, instances cannot be assigned new variables not
1838+
* Without a :attr:`~object.__dict__` variable, instances cannot be assigned new
1839+
variables not
18311840
listed in the *__slots__* definition. Attempts to assign to an unlisted
18321841
variable name raises :exc:`AttributeError`. If dynamic assignment of new
18331842
variables is desired, then add ``'__dict__'`` to the sequence of strings in
18341843
the *__slots__* declaration.
18351844

18361845
* Without a *__weakref__* variable for each instance, classes defining
1837-
*__slots__* do not support weak references to its instances. If weak reference
1846+
*__slots__* do not support :mod:`weak references <weakref>` to its instances.
1847+
If weak reference
18381848
support is needed, then add ``'__weakref__'`` to the sequence of strings in the
18391849
*__slots__* declaration.
18401850

1841-
* *__slots__* are implemented at the class level by creating descriptors
1842-
(:ref:`descriptors`) for each variable name. As a result, class attributes
1851+
* *__slots__* are implemented at the class level by creating :ref:`descriptors <descriptors>`
1852+
for each variable name. As a result, class attributes
18431853
cannot be used to set default values for instance variables defined by
18441854
*__slots__*; otherwise, the class attribute would overwrite the descriptor
18451855
assignment.
18461856

18471857
* The action of a *__slots__* declaration is not limited to the class
18481858
where it is defined. *__slots__* declared in parents are available in
1849-
child classes. However, child subclasses will get a *__dict__* and
1859+
child classes. However, child subclasses will get a :attr:`~object.__dict__` and
18501860
*__weakref__* unless they also define *__slots__* (which should only
18511861
contain names of any *additional* slots).
18521862

@@ -1862,14 +1872,17 @@ Notes on using *__slots__*
18621872
used; however, in the future, special meaning may be assigned to the values
18631873
corresponding to each key.
18641874

1865-
* *__class__* assignment works only if both classes have the same *__slots__*.
1875+
* :attr:`~instance.__class__` assignment works only if both classes have the
1876+
same *__slots__*.
18661877

1867-
* Multiple inheritance with multiple slotted parent classes can be used,
1878+
* :ref:`Multiple inheritance <tut-multiple>` with multiple slotted parent
1879+
classes can be used,
18681880
but only one parent is allowed to have attributes created by slots
18691881
(the other bases must have empty slot layouts) - violations raise
18701882
:exc:`TypeError`.
18711883

1872-
* If an iterator is used for *__slots__* then a descriptor is created for each
1884+
* If an :term:`iterator` is used for *__slots__* then a :term:`descriptor` is
1885+
created for each
18731886
of the iterator's values. However, the *__slots__* attribute will be an empty
18741887
iterator.
18751888

@@ -1878,7 +1891,7 @@ Notes on using *__slots__*
18781891
Customizing class creation
18791892
--------------------------
18801893

1881-
Whenever a class inherits from another class, *__init_subclass__* is
1894+
Whenever a class inherits from another class, :meth:`~object.__init_subclass__` is
18821895
called on that class. This way, it is possible to write classes which
18831896
change the behavior of subclasses. This is closely related to class
18841897
decorators, but where class decorators only affect the specific class they're
@@ -2004,7 +2017,8 @@ Once the appropriate metaclass has been identified, then the class namespace
20042017
is prepared. If the metaclass has a ``__prepare__`` attribute, it is called
20052018
as ``namespace = metaclass.__prepare__(name, bases, **kwds)`` (where the
20062019
additional keyword arguments, if any, come from the class definition). The
2007-
``__prepare__`` method should be implemented as a :func:`classmethod`. The
2020+
``__prepare__`` method should be implemented as a
2021+
:func:`classmethod <classmethod>`. The
20082022
namespace returned by ``__prepare__`` is passed in to ``__new__``, but when
20092023
the final class object is created the namespace is copied into a new ``dict``.
20102024

@@ -2302,31 +2316,36 @@ Emulating container types
23022316
-------------------------
23032317

23042318
The following methods can be defined to implement container objects. Containers
2305-
usually are sequences (such as lists or tuples) or mappings (like dictionaries),
2319+
usually are :term:`sequences <sequence>` (such as :class:`lists <list>` or
2320+
:class:`tuples <tuple>`) or :term:`mappings <mapping>` (like
2321+
:class:`dictionaries <dict>`),
23062322
but can represent other containers as well. The first set of methods is used
23072323
either to emulate a sequence or to emulate a mapping; the difference is that for
23082324
a sequence, the allowable keys should be the integers *k* for which ``0 <= k <
2309-
N`` where *N* is the length of the sequence, or slice objects, which define a
2325+
N`` where *N* is the length of the sequence, or :class:`slice` objects, which define a
23102326
range of items. It is also recommended that mappings provide the methods
23112327
:meth:`keys`, :meth:`values`, :meth:`items`, :meth:`get`, :meth:`clear`,
23122328
:meth:`setdefault`, :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and
2313-
:meth:`update` behaving similar to those for Python's standard dictionary
2329+
:meth:`update` behaving similar to those for Python's standard :class:`dictionary <dict>`
23142330
objects. The :mod:`collections.abc` module provides a
23152331
:class:`~collections.abc.MutableMapping`
2316-
abstract base class to help create those methods from a base set of
2317-
:meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`, and :meth:`keys`.
2332+
:term:`abstract base class` to help create those methods from a base set of
2333+
:meth:`~object.__getitem__`, :meth:`~object.__setitem__`, :meth:`~object.__delitem__`, and :meth:`keys`.
23182334
Mutable sequences should provide methods :meth:`append`, :meth:`count`,
23192335
:meth:`index`, :meth:`extend`, :meth:`insert`, :meth:`pop`, :meth:`remove`,
2320-
:meth:`reverse` and :meth:`sort`, like Python standard list objects. Finally,
2336+
:meth:`reverse` and :meth:`sort`, like Python standard :class:`list`
2337+
objects. Finally,
23212338
sequence types should implement addition (meaning concatenation) and
2322-
multiplication (meaning repetition) by defining the methods :meth:`__add__`,
2323-
:meth:`__radd__`, :meth:`__iadd__`, :meth:`__mul__`, :meth:`__rmul__` and
2324-
:meth:`__imul__` described below; they should not define other numerical
2339+
multiplication (meaning repetition) by defining the methods
2340+
:meth:`~object.__add__`, :meth:`~object.__radd__`, :meth:`~object.__iadd__`,
2341+
:meth:`~object.__mul__`, :meth:`~object.__rmul__` and :meth:`~object.__imul__`
2342+
described below; they should not define other numerical
23252343
operators. It is recommended that both mappings and sequences implement the
2326-
:meth:`__contains__` method to allow efficient use of the ``in`` operator; for
2344+
:meth:`~object.__contains__` method to allow efficient use of the ``in``
2345+
operator; for
23272346
mappings, ``in`` should search the mapping's keys; for sequences, it should
23282347
search through the values. It is further recommended that both mappings and
2329-
sequences implement the :meth:`__iter__` method to allow efficient iteration
2348+
sequences implement the :meth:`~object.__iter__` method to allow efficient iteration
23302349
through the container; for mappings, :meth:`__iter__` should iterate
23312350
through the object's keys; for sequences, it should iterate through the values.
23322351

@@ -2715,7 +2734,8 @@ exception::
27152734
TypeError: object of type 'C' has no len()
27162735

27172736
The rationale behind this behaviour lies with a number of special methods such
2718-
as :meth:`__hash__` and :meth:`__repr__` that are implemented by all objects,
2737+
as :meth:`~object.__hash__` and :meth:`~object.__repr__` that are implemented
2738+
by all objects,
27192739
including type objects. If the implicit lookup of these methods used the
27202740
conventional lookup process, they would fail when invoked on the type object
27212741
itself::
@@ -2738,7 +2758,7 @@ the instance when looking up special methods::
27382758

27392759
In addition to bypassing any instance attributes in the interest of
27402760
correctness, implicit special method lookup generally also bypasses the
2741-
:meth:`__getattribute__` method even of the object's metaclass::
2761+
:meth:`~object.__getattribute__` method even of the object's metaclass::
27422762

27432763
>>> class Meta(type):
27442764
... def __getattribute__(*args):
@@ -2762,7 +2782,7 @@ correctness, implicit special method lookup generally also bypasses the
27622782
>>> len(c) # Implicit lookup
27632783
10
27642784

2765-
Bypassing the :meth:`__getattribute__` machinery in this fashion
2785+
Bypassing the :meth:`~object.__getattribute__` machinery in this fashion
27662786
provides significant scope for speed optimisations within the
27672787
interpreter, at the cost of some flexibility in the handling of
27682788
special methods (the special method *must* be set on the class
@@ -2779,15 +2799,15 @@ Coroutines
27792799
Awaitable Objects
27802800
-----------------
27812801

2782-
An :term:`awaitable` object generally implements an :meth:`__await__` method.
2802+
An :term:`awaitable` object generally implements an :meth:`~object.__await__` method.
27832803
:term:`Coroutine objects <coroutine>` returned from :keyword:`async def` functions
27842804
are awaitable.
27852805

27862806
.. note::
27872807

27882808
The :term:`generator iterator` objects returned from generators
27892809
decorated with :func:`types.coroutine` or :func:`asyncio.coroutine`
2790-
are also awaitable, but they do not implement :meth:`__await__`.
2810+
are also awaitable, but they do not implement :meth:`~object.__await__`.
27912811

27922812
.. method:: object.__await__(self)
27932813

@@ -2806,7 +2826,7 @@ Coroutine Objects
28062826
-----------------
28072827

28082828
:term:`Coroutine objects <coroutine>` are :term:`awaitable` objects.
2809-
A coroutine's execution can be controlled by calling :meth:`__await__` and
2829+
A coroutine's execution can be controlled by calling :meth:`~object.__await__` and
28102830
iterating over the result. When the coroutine has finished executing and
28112831
returns, the iterator raises :exc:`StopIteration`, and the exception's
28122832
:attr:`~StopIteration.value` attribute holds the return value. If the
@@ -2825,7 +2845,7 @@ generators, coroutines do not directly support iteration.
28252845

28262846
Starts or resumes execution of the coroutine. If *value* is ``None``,
28272847
this is equivalent to advancing the iterator returned by
2828-
:meth:`__await__`. If *value* is not ``None``, this method delegates
2848+
:meth:`~object.__await__`. If *value* is not ``None``, this method delegates
28292849
to the :meth:`~generator.send` method of the iterator that caused
28302850
the coroutine to suspend. The result (return value,
28312851
:exc:`StopIteration`, or other exception) is the same as when
@@ -2838,7 +2858,7 @@ generators, coroutines do not directly support iteration.
28382858
the coroutine to suspend, if it has such a method. Otherwise,
28392859
the exception is raised at the suspension point. The result
28402860
(return value, :exc:`StopIteration`, or other exception) is the same as
2841-
when iterating over the :meth:`__await__` return value, described
2861+
when iterating over the :meth:`~object.__await__` return value, described
28422862
above. If the exception is not caught in the coroutine, it propagates
28432863
back to the caller.
28442864

@@ -2892,11 +2912,11 @@ An example of an asynchronous iterable object::
28922912
.. versionadded:: 3.5
28932913

28942914
.. versionchanged:: 3.7
2895-
Prior to Python 3.7, ``__aiter__`` could return an *awaitable*
2915+
Prior to Python 3.7, :meth:`~object.__aiter__` could return an *awaitable*
28962916
that would resolve to an
28972917
:term:`asynchronous iterator <asynchronous iterator>`.
28982918

2899-
Starting with Python 3.7, ``__aiter__`` must return an
2919+
Starting with Python 3.7, :meth:`~object.__aiter__` must return an
29002920
asynchronous iterator object. Returning anything else
29012921
will result in a :exc:`TypeError` error.
29022922

@@ -2939,8 +2959,9 @@ An example of an asynchronous context manager class::
29392959
controlled conditions. It generally isn't a good idea though, since it can
29402960
lead to some very strange behaviour if it is handled incorrectly.
29412961
2942-
.. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
2943-
:meth:`__contains__` methods have special handling for this; others
2962+
.. [#] The :meth:`~object.__hash__`, :meth:`~object.__iter__`,
2963+
:meth:`~object.__reversed__`, and :meth:`~object.__contains__` methods have
2964+
special handling for this; others
29442965
will still raise a :exc:`TypeError`, but may do so by relying on
29452966
the behavior that ``None`` is not callable.
29462967
@@ -2951,5 +2972,6 @@ An example of an asynchronous context manager class::
29512972
*blocking* such fallback.
29522973
29532974
.. [#] For operands of the same type, it is assumed that if the non-reflected
2954-
method -- such as :meth:`__add__` -- fails then the overall operation is not
2975+
method -- such as :meth:`~object.__add__` -- fails then the overall
2976+
operation is not
29552977
supported, which is why the reflected method is not called.
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)