@@ -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
@@ -677,7 +677,8 @@ Callable types
677
677
returns an :term: `asynchronous iterator ` object which can be used in an
678
678
:keyword: `async for ` statement to execute the body of the function.
679
679
680
- Calling the asynchronous iterator's :meth: `aiterator.__anext__ ` method
680
+ Calling the asynchronous iterator's
681
+ :meth: `aiterator.__anext__ <object.__anext__> ` method
681
682
will return an :term: `awaitable ` which when awaited
682
683
will execute until it provides a value using the :keyword: `yield `
683
684
expression. When the function executes an empty :keyword: `return `
@@ -715,13 +716,13 @@ Callable types
715
716
Classes
716
717
Classes are callable. These objects normally act as factories for new
717
718
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
720
721
initialize the new instance.
721
722
722
723
Class Instances
723
724
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.
725
726
726
727
727
728
Modules
@@ -880,14 +881,14 @@ Class instances
880
881
section :ref: `descriptors ` for another way in which attributes of a class
881
882
retrieved via its instances may differ from the objects actually stored in
882
883
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
884
885
the lookup.
885
886
886
887
.. index :: triple: class instance; attribute; assignment
887
888
888
889
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
891
892
dictionary directly.
892
893
893
894
.. index ::
@@ -1141,7 +1142,8 @@ Internal types
1141
1142
Slice objects
1142
1143
.. index :: builtin: slice
1143
1144
1144
- Slice objects are used to represent slices for :meth: `__getitem__ `
1145
+ Slice objects are used to represent slices for
1146
+ :meth: `~object.__getitem__ `
1145
1147
methods. They are also created by the built-in :func: `slice ` function.
1146
1148
1147
1149
.. index ::
@@ -1194,17 +1196,18 @@ A class can implement certain operations that are invoked by special syntax
1194
1196
(such as arithmetic operations or subscripting and slicing) by defining methods
1195
1197
with special names. This is Python's approach to :dfn: `operator overloading `,
1196
1198
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__ `,
1198
1201
and ``x `` is an instance of this class, then ``x[i] `` is roughly equivalent
1199
1202
to ``type(x).__getitem__(x, i) ``. Except where mentioned, attempts to execute an
1200
1203
operation raise an exception when no appropriate method is defined (typically
1201
1204
:exc: `AttributeError ` or :exc: `TypeError `).
1202
1205
1203
1206
Setting a special method to ``None `` indicates that the corresponding
1204
1207
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
1206
1209
:func: `iter ` on its instances will raise a :exc: `TypeError ` (without
1207
- falling back to :meth: `__getitem__ `). [# ]_
1210
+ falling back to :meth: `~object. __getitem__ `). [# ]_
1208
1211
1209
1212
When implementing a class that emulates any built-in type, it is important that
1210
1213
the emulation only be implemented to the degree that it makes sense for the
@@ -1754,7 +1757,8 @@ Invoking Descriptors
1754
1757
1755
1758
In general, a descriptor is an object attribute with "binding behavior", one
1756
1759
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
1758
1762
those methods are defined for an object, it is said to be a descriptor.
1759
1763
1760
1764
The default behavior for attribute access is to get, set, or delete the
@@ -1790,7 +1794,8 @@ Super Binding
1790
1794
1791
1795
For instance bindings, the precedence of descriptor invocation depends on
1792
1796
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
1794
1799
define :meth: `__get__ `, then accessing the attribute will return the descriptor
1795
1800
object itself unless there is a value in the object's instance dictionary. If
1796
1801
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
1801
1806
instance dictionary. In contrast, non-data descriptors can be overridden by
1802
1807
instances.
1803
1808
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
1805
1811
implemented as non-data descriptors. Accordingly, instances can redefine and
1806
1812
override methods. This allows individual instances to acquire behaviors that
1807
1813
differ from other instances of the same class.
@@ -1816,46 +1822,50 @@ __slots__
1816
1822
^^^^^^^^^
1817
1823
1818
1824
*__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__ *
1820
1826
(unless explicitly declared in *__slots__ * or available in a parent.)
1821
1827
1822
- The space saved over using * __dict__ * can be significant.
1828
+ The space saved over using :attr: ` ~object. __dict__` can be significant.
1823
1829
Attribute lookup speed can be significantly improved as well.
1824
1830
1825
1831
.. data :: object.__slots__
1826
1832
1827
1833
This class variable can be assigned a string, iterable, or sequence of
1828
1834
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__ `
1830
1837
and *__weakref__ * for each instance.
1831
1838
1832
1839
1833
1840
Notes on using *__slots__ *
1834
1841
""""""""""""""""""""""""""
1835
1842
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
1837
1845
*__weakref__ * attribute of the instances will always be accessible.
1838
1846
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
1840
1849
listed in the *__slots__ * definition. Attempts to assign to an unlisted
1841
1850
variable name raises :exc: `AttributeError `. If dynamic assignment of new
1842
1851
variables is desired, then add ``'__dict__' `` to the sequence of strings in
1843
1852
the *__slots__ * declaration.
1844
1853
1845
1854
* 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
1847
1857
support is needed, then add ``'__weakref__' `` to the sequence of strings in the
1848
1858
*__slots__ * declaration.
1849
1859
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
1852
1862
cannot be used to set default values for instance variables defined by
1853
1863
*__slots__ *; otherwise, the class attribute would overwrite the descriptor
1854
1864
assignment.
1855
1865
1856
1866
* The action of a *__slots__ * declaration is not limited to the class
1857
1867
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
1859
1869
*__weakref__ * unless they also define *__slots__ * (which should only
1860
1870
contain names of any *additional * slots).
1861
1871
@@ -1871,14 +1881,17 @@ Notes on using *__slots__*
1871
1881
used; however, in the future, special meaning may be assigned to the values
1872
1882
corresponding to each key.
1873
1883
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__ *.
1875
1886
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,
1877
1889
but only one parent is allowed to have attributes created by slots
1878
1890
(the other bases must have empty slot layouts) - violations raise
1879
1891
:exc: `TypeError `.
1880
1892
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
1882
1895
of the iterator's values. However, the *__slots__ * attribute will be an empty
1883
1896
iterator.
1884
1897
@@ -1887,7 +1900,7 @@ Notes on using *__slots__*
1887
1900
Customizing class creation
1888
1901
--------------------------
1889
1902
1890
- Whenever a class inherits from another class, * __init_subclass__ * is
1903
+ Whenever a class inherits from another class, :meth: ` ~object. __init_subclass__` is
1891
1904
called on that class. This way, it is possible to write classes which
1892
1905
change the behavior of subclasses. This is closely related to class
1893
1906
decorators, but where class decorators only affect the specific class they're
@@ -1928,7 +1941,7 @@ class defining the method.
1928
1941
1929
1942
1930
1943
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.
1932
1945
1933
1946
.. method :: object.__set_name__(self, owner, name)
1934
1947
@@ -2040,7 +2053,8 @@ Once the appropriate metaclass has been identified, then the class namespace
2040
2053
is prepared. If the metaclass has a ``__prepare__ `` attribute, it is called
2041
2054
as ``namespace = metaclass.__prepare__(name, bases, **kwds) `` (where the
2042
2055
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
2044
2058
namespace returned by ``__prepare__ `` is passed in to ``__new__ ``, but when
2045
2059
the final class object is created the namespace is copied into a new ``dict ``.
2046
2060
@@ -2338,31 +2352,36 @@ Emulating container types
2338
2352
-------------------------
2339
2353
2340
2354
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> `),
2342
2358
but can represent other containers as well. The first set of methods is used
2343
2359
either to emulate a sequence or to emulate a mapping; the difference is that for
2344
2360
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
2346
2362
range of items. It is also recommended that mappings provide the methods
2347
2363
:meth: `keys `, :meth: `values `, :meth: `items `, :meth: `get `, :meth: `clear `,
2348
2364
: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> `
2350
2366
objects. The :mod: `collections.abc ` module provides a
2351
2367
: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 `.
2354
2370
Mutable sequences should provide methods :meth: `append `, :meth: `count `,
2355
2371
: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,
2357
2374
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
2361
2379
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
2363
2382
mappings, ``in `` should search the mapping's keys; for sequences, it should
2364
2383
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
2366
2385
through the container; for mappings, :meth: `__iter__ ` should iterate
2367
2386
through the object's keys; for sequences, it should iterate through the values.
2368
2387
@@ -2774,7 +2793,8 @@ exception::
2774
2793
TypeError: object of type 'C' has no len()
2775
2794
2776
2795
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,
2778
2798
including type objects. If the implicit lookup of these methods used the
2779
2799
conventional lookup process, they would fail when invoked on the type object
2780
2800
itself::
@@ -2797,7 +2817,7 @@ the instance when looking up special methods::
2797
2817
2798
2818
In addition to bypassing any instance attributes in the interest of
2799
2819
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::
2801
2821
2802
2822
>>> class Meta(type):
2803
2823
... def __getattribute__(*args):
@@ -2821,7 +2841,7 @@ correctness, implicit special method lookup generally also bypasses the
2821
2841
>>> len(c) # Implicit lookup
2822
2842
10
2823
2843
2824
- Bypassing the :meth: `__getattribute__ ` machinery in this fashion
2844
+ Bypassing the :meth: `~object. __getattribute__ ` machinery in this fashion
2825
2845
provides significant scope for speed optimisations within the
2826
2846
interpreter, at the cost of some flexibility in the handling of
2827
2847
special methods (the special method *must * be set on the class
@@ -2838,15 +2858,15 @@ Coroutines
2838
2858
Awaitable Objects
2839
2859
-----------------
2840
2860
2841
- An :term: `awaitable ` object generally implements an :meth: `__await__ ` method.
2861
+ An :term: `awaitable ` object generally implements an :meth: `~object. __await__ ` method.
2842
2862
:term: `Coroutine objects <coroutine> ` returned from :keyword: `async def ` functions
2843
2863
are awaitable.
2844
2864
2845
2865
.. note ::
2846
2866
2847
2867
The :term: `generator iterator ` objects returned from generators
2848
2868
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__ `.
2850
2870
2851
2871
.. method :: object.__await__(self)
2852
2872
@@ -2865,7 +2885,7 @@ Coroutine Objects
2865
2885
-----------------
2866
2886
2867
2887
: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
2869
2889
iterating over the result. When the coroutine has finished executing and
2870
2890
returns, the iterator raises :exc: `StopIteration `, and the exception's
2871
2891
:attr: `~StopIteration.value ` attribute holds the return value. If the
@@ -2884,7 +2904,7 @@ generators, coroutines do not directly support iteration.
2884
2904
2885
2905
Starts or resumes execution of the coroutine. If *value * is ``None ``,
2886
2906
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
2888
2908
to the :meth: `~generator.send ` method of the iterator that caused
2889
2909
the coroutine to suspend. The result (return value,
2890
2910
:exc: `StopIteration `, or other exception) is the same as when
@@ -2897,7 +2917,7 @@ generators, coroutines do not directly support iteration.
2897
2917
the coroutine to suspend, if it has such a method. Otherwise,
2898
2918
the exception is raised at the suspension point. The result
2899
2919
(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
2901
2921
above. If the exception is not caught in the coroutine, it propagates
2902
2922
back to the caller.
2903
2923
@@ -2951,11 +2971,11 @@ An example of an asynchronous iterable object::
2951
2971
.. versionadded :: 3.5
2952
2972
2953
2973
.. 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 *
2955
2975
that would resolve to an
2956
2976
:term: `asynchronous iterator <asynchronous iterator> `.
2957
2977
2958
- Starting with Python 3.7, `` __aiter__ ` ` must return an
2978
+ Starting with Python 3.7, :meth: ` ~object. __aiter__ ` must return an
2959
2979
asynchronous iterator object. Returning anything else
2960
2980
will result in a :exc: `TypeError ` error.
2961
2981
@@ -2998,8 +3018,9 @@ An example of an asynchronous context manager class::
2998
3018
controlled conditions. It generally isn't a good idea though, since it can
2999
3019
lead to some very strange behaviour if it is handled incorrectly.
3000
3020
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
3003
3024
will still raise a :exc: `TypeError `, but may do so by relying on
3004
3025
the behavior that ``None `` is not callable.
3005
3026
@@ -3010,5 +3031,6 @@ An example of an asynchronous context manager class::
3010
3031
*blocking * such fallback.
3011
3032
3012
3033
.. [# ] 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
3014
3036
supported, which is why the reflected method is not called.
0 commit comments