@@ -188,7 +188,7 @@ Ellipsis
188
188
representation in computers.
189
189
190
190
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
192
192
properties:
193
193
194
194
* They are valid numeric literals which, when passed to their
@@ -674,7 +674,8 @@ Callable types
674
674
returns an asynchronous iterator object which can be used in an
675
675
:keyword: `async for ` statement to execute the body of the function.
676
676
677
- Calling the asynchronous iterator's :meth: `aiterator.__anext__ ` method
677
+ Calling the asynchronous iterator's
678
+ :meth: `aiterator.__anext__ <object.__anext__> ` method
678
679
will return an :term: `awaitable ` which when awaited
679
680
will execute until it provides a value using the :keyword: `yield `
680
681
expression. When the function executes an empty :keyword: `return `
@@ -712,13 +713,13 @@ Callable types
712
713
Classes
713
714
Classes are callable. These objects normally act as factories for new
714
715
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
717
718
initialize the new instance.
718
719
719
720
Class Instances
720
721
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.
722
723
723
724
724
725
Modules
@@ -848,14 +849,14 @@ Class instances
848
849
section :ref: `descriptors ` for another way in which attributes of a class
849
850
retrieved via its instances may differ from the objects actually stored in
850
851
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
852
853
the lookup.
853
854
854
855
.. index :: triple: class instance; attribute; assignment
855
856
856
857
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
859
860
dictionary directly.
860
861
861
862
.. index ::
@@ -1109,7 +1110,8 @@ Internal types
1109
1110
Slice objects
1110
1111
.. index :: builtin: slice
1111
1112
1112
- Slice objects are used to represent slices for :meth: `__getitem__ `
1113
+ Slice objects are used to represent slices for
1114
+ :meth: `~object.__getitem__ `
1113
1115
methods. They are also created by the built-in :func: `slice ` function.
1114
1116
1115
1117
.. index ::
@@ -1163,17 +1165,18 @@ A class can implement certain operations that are invoked by special syntax
1163
1165
(such as arithmetic operations or subscripting and slicing) by defining methods
1164
1166
with special names. This is Python's approach to :dfn: `operator overloading `,
1165
1167
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__ `,
1167
1170
and ``x `` is an instance of this class, then ``x[i] `` is roughly equivalent
1168
1171
to ``type(x).__getitem__(x, i) ``. Except where mentioned, attempts to execute an
1169
1172
operation raise an exception when no appropriate method is defined (typically
1170
1173
:exc: `AttributeError ` or :exc: `TypeError `).
1171
1174
1172
1175
Setting a special method to ``None `` indicates that the corresponding
1173
1176
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
1175
1178
:func: `iter ` on its instances will raise a :exc: `TypeError ` (without
1176
- falling back to :meth: `__getitem__ `). [# ]_
1179
+ falling back to :meth: `~object. __getitem__ `). [# ]_
1177
1180
1178
1181
When implementing a class that emulates any built-in type, it is important that
1179
1182
the emulation only be implemented to the degree that it makes sense for the
@@ -1745,7 +1748,8 @@ Invoking Descriptors
1745
1748
1746
1749
In general, a descriptor is an object attribute with "binding behavior", one
1747
1750
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
1749
1753
those methods are defined for an object, it is said to be a descriptor.
1750
1754
1751
1755
The default behavior for attribute access is to get, set, or delete the
@@ -1781,7 +1785,8 @@ Super Binding
1781
1785
1782
1786
For instance bindings, the precedence of descriptor invocation depends on
1783
1787
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
1785
1790
define :meth: `__get__ `, then accessing the attribute will return the descriptor
1786
1791
object itself unless there is a value in the object's instance dictionary. If
1787
1792
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
1792
1797
instance dictionary. In contrast, non-data descriptors can be overridden by
1793
1798
instances.
1794
1799
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
1796
1802
implemented as non-data descriptors. Accordingly, instances can redefine and
1797
1803
override methods. This allows individual instances to acquire behaviors that
1798
1804
differ from other instances of the same class.
@@ -1807,46 +1813,50 @@ __slots__
1807
1813
^^^^^^^^^
1808
1814
1809
1815
*__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__ *
1811
1817
(unless explicitly declared in *__slots__ * or available in a parent.)
1812
1818
1813
- The space saved over using * __dict__ * can be significant.
1819
+ The space saved over using :attr: ` ~object. __dict__` can be significant.
1814
1820
Attribute lookup speed can be significantly improved as well.
1815
1821
1816
1822
.. data :: object.__slots__
1817
1823
1818
1824
This class variable can be assigned a string, iterable, or sequence of
1819
1825
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__ `
1821
1828
and *__weakref__ * for each instance.
1822
1829
1823
1830
1824
1831
Notes on using *__slots__ *
1825
1832
""""""""""""""""""""""""""
1826
1833
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
1828
1836
*__weakref__ * attribute of the instances will always be accessible.
1829
1837
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
1831
1840
listed in the *__slots__ * definition. Attempts to assign to an unlisted
1832
1841
variable name raises :exc: `AttributeError `. If dynamic assignment of new
1833
1842
variables is desired, then add ``'__dict__' `` to the sequence of strings in
1834
1843
the *__slots__ * declaration.
1835
1844
1836
1845
* 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
1838
1848
support is needed, then add ``'__weakref__' `` to the sequence of strings in the
1839
1849
*__slots__ * declaration.
1840
1850
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
1843
1853
cannot be used to set default values for instance variables defined by
1844
1854
*__slots__ *; otherwise, the class attribute would overwrite the descriptor
1845
1855
assignment.
1846
1856
1847
1857
* The action of a *__slots__ * declaration is not limited to the class
1848
1858
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
1850
1860
*__weakref__ * unless they also define *__slots__ * (which should only
1851
1861
contain names of any *additional * slots).
1852
1862
@@ -1862,14 +1872,17 @@ Notes on using *__slots__*
1862
1872
used; however, in the future, special meaning may be assigned to the values
1863
1873
corresponding to each key.
1864
1874
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__ *.
1866
1877
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,
1868
1880
but only one parent is allowed to have attributes created by slots
1869
1881
(the other bases must have empty slot layouts) - violations raise
1870
1882
:exc: `TypeError `.
1871
1883
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
1873
1886
of the iterator's values. However, the *__slots__ * attribute will be an empty
1874
1887
iterator.
1875
1888
@@ -1878,7 +1891,7 @@ Notes on using *__slots__*
1878
1891
Customizing class creation
1879
1892
--------------------------
1880
1893
1881
- Whenever a class inherits from another class, * __init_subclass__ * is
1894
+ Whenever a class inherits from another class, :meth: ` ~object. __init_subclass__` is
1882
1895
called on that class. This way, it is possible to write classes which
1883
1896
change the behavior of subclasses. This is closely related to class
1884
1897
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
2004
2017
is prepared. If the metaclass has a ``__prepare__ `` attribute, it is called
2005
2018
as ``namespace = metaclass.__prepare__(name, bases, **kwds) `` (where the
2006
2019
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
2008
2022
namespace returned by ``__prepare__ `` is passed in to ``__new__ ``, but when
2009
2023
the final class object is created the namespace is copied into a new ``dict ``.
2010
2024
@@ -2302,31 +2316,36 @@ Emulating container types
2302
2316
-------------------------
2303
2317
2304
2318
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> `),
2306
2322
but can represent other containers as well. The first set of methods is used
2307
2323
either to emulate a sequence or to emulate a mapping; the difference is that for
2308
2324
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
2310
2326
range of items. It is also recommended that mappings provide the methods
2311
2327
:meth: `keys `, :meth: `values `, :meth: `items `, :meth: `get `, :meth: `clear `,
2312
2328
: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> `
2314
2330
objects. The :mod: `collections.abc ` module provides a
2315
2331
: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 `.
2318
2334
Mutable sequences should provide methods :meth: `append `, :meth: `count `,
2319
2335
: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,
2321
2338
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
2325
2343
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
2327
2346
mappings, ``in `` should search the mapping's keys; for sequences, it should
2328
2347
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
2330
2349
through the container; for mappings, :meth: `__iter__ ` should iterate
2331
2350
through the object's keys; for sequences, it should iterate through the values.
2332
2351
@@ -2715,7 +2734,8 @@ exception::
2715
2734
TypeError: object of type 'C' has no len()
2716
2735
2717
2736
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,
2719
2739
including type objects. If the implicit lookup of these methods used the
2720
2740
conventional lookup process, they would fail when invoked on the type object
2721
2741
itself::
@@ -2738,7 +2758,7 @@ the instance when looking up special methods::
2738
2758
2739
2759
In addition to bypassing any instance attributes in the interest of
2740
2760
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::
2742
2762
2743
2763
>>> class Meta(type):
2744
2764
... def __getattribute__(*args):
@@ -2762,7 +2782,7 @@ correctness, implicit special method lookup generally also bypasses the
2762
2782
>>> len(c) # Implicit lookup
2763
2783
10
2764
2784
2765
- Bypassing the :meth: `__getattribute__ ` machinery in this fashion
2785
+ Bypassing the :meth: `~object. __getattribute__ ` machinery in this fashion
2766
2786
provides significant scope for speed optimisations within the
2767
2787
interpreter, at the cost of some flexibility in the handling of
2768
2788
special methods (the special method *must * be set on the class
@@ -2779,15 +2799,15 @@ Coroutines
2779
2799
Awaitable Objects
2780
2800
-----------------
2781
2801
2782
- An :term: `awaitable ` object generally implements an :meth: `__await__ ` method.
2802
+ An :term: `awaitable ` object generally implements an :meth: `~object. __await__ ` method.
2783
2803
:term: `Coroutine objects <coroutine> ` returned from :keyword: `async def ` functions
2784
2804
are awaitable.
2785
2805
2786
2806
.. note ::
2787
2807
2788
2808
The :term: `generator iterator ` objects returned from generators
2789
2809
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__ `.
2791
2811
2792
2812
.. method :: object.__await__(self)
2793
2813
@@ -2806,7 +2826,7 @@ Coroutine Objects
2806
2826
-----------------
2807
2827
2808
2828
: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
2810
2830
iterating over the result. When the coroutine has finished executing and
2811
2831
returns, the iterator raises :exc: `StopIteration `, and the exception's
2812
2832
:attr: `~StopIteration.value ` attribute holds the return value. If the
@@ -2825,7 +2845,7 @@ generators, coroutines do not directly support iteration.
2825
2845
2826
2846
Starts or resumes execution of the coroutine. If *value * is ``None ``,
2827
2847
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
2829
2849
to the :meth: `~generator.send ` method of the iterator that caused
2830
2850
the coroutine to suspend. The result (return value,
2831
2851
:exc: `StopIteration `, or other exception) is the same as when
@@ -2838,7 +2858,7 @@ generators, coroutines do not directly support iteration.
2838
2858
the coroutine to suspend, if it has such a method. Otherwise,
2839
2859
the exception is raised at the suspension point. The result
2840
2860
(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
2842
2862
above. If the exception is not caught in the coroutine, it propagates
2843
2863
back to the caller.
2844
2864
@@ -2892,11 +2912,11 @@ An example of an asynchronous iterable object::
2892
2912
.. versionadded :: 3.5
2893
2913
2894
2914
.. 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 *
2896
2916
that would resolve to an
2897
2917
:term: `asynchronous iterator <asynchronous iterator> `.
2898
2918
2899
- Starting with Python 3.7, `` __aiter__ ` ` must return an
2919
+ Starting with Python 3.7, :meth: ` ~object. __aiter__ ` must return an
2900
2920
asynchronous iterator object. Returning anything else
2901
2921
will result in a :exc: `TypeError ` error.
2902
2922
@@ -2939,8 +2959,9 @@ An example of an asynchronous context manager class::
2939
2959
controlled conditions. It generally isn't a good idea though, since it can
2940
2960
lead to some very strange behaviour if it is handled incorrectly.
2941
2961
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
2944
2965
will still raise a :exc: `TypeError `, but may do so by relying on
2945
2966
the behavior that ``None `` is not callable.
2946
2967
@@ -2951,5 +2972,6 @@ An example of an asynchronous context manager class::
2951
2972
*blocking * such fallback.
2952
2973
2953
2974
.. [# ] 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
2955
2977
supported, which is why the reflected method is not called.
0 commit comments