Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Side by Side Diff: Doc/reference/datamodel.rst

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 4 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/collections.abc.rst ('k') | Lib/_collections_abc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 .. _datamodel: 2 .. _datamodel:
3 3
4 ********** 4 **********
5 Data model 5 Data model
6 ********** 6 **********
7 7
8 8
9 .. _objects: 9 .. _objects:
10 10
(...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 1056
1057 A class can implement certain operations that are invoked by special syntax 1057 A class can implement certain operations that are invoked by special syntax
1058 (such as arithmetic operations or subscripting and slicing) by defining methods 1058 (such as arithmetic operations or subscripting and slicing) by defining methods
1059 with special names. This is Python's approach to :dfn:`operator overloading`, 1059 with special names. This is Python's approach to :dfn:`operator overloading`,
1060 allowing classes to define their own behavior with respect to language 1060 allowing classes to define their own behavior with respect to language
1061 operators. For instance, if a class defines a method named :meth:`__getitem__`, 1061 operators. For instance, if a class defines a method named :meth:`__getitem__`,
1062 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent 1062 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent
1063 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n 1063 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n
1064 operation raise an exception when no appropriate method is defined (typically 1064 operation raise an exception when no appropriate method is defined (typically
1065 :exc:`AttributeError` or :exc:`TypeError`). 1065 :exc:`AttributeError` or :exc:`TypeError`).
1066
1067 Setting a special method to ``None`` indicates that the corresponding
1068 operation is not available. For example, if a class sets
1069 :meth:`__iter__` to ``None``, the class is not iterable, so calling
1070 :func:`iter` on its instances will raise a :exc:`TypeError` (without
1071 falling back to :meth:`__getitem__`). [#]_
1066 1072
1067 When implementing a class that emulates any built-in type, it is important that 1073 When implementing a class that emulates any built-in type, it is important that
1068 the emulation only be implemented to the degree that it makes sense for the 1074 the emulation only be implemented to the degree that it makes sense for the
1069 object being modelled. For example, some sequences may work well with retrieval 1075 object being modelled. For example, some sequences may work well with retrieval
1070 of individual elements, but extracting a slice may not make sense. (One example 1076 of individual elements, but extracting a slice may not make sense. (One example
1071 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document 1077 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document
1072 Object Model.) 1078 Object Model.)
1073 1079
1074 1080
1075 .. _customization: 1081 .. _customization:
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after
2048 object.__ror__(self, other) 2054 object.__ror__(self, other)
2049 2055
2050 .. index:: 2056 .. index::
2051 builtin: divmod 2057 builtin: divmod
2052 builtin: pow 2058 builtin: pow
2053 2059
2054 These methods are called to implement the binary arithmetic operations 2060 These methods are called to implement the binary arithmetic operations
2055 (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`, 2061 (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
2056 :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected 2062 :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected
2057 (swapped) operands. These functions are only called if the left operand does 2063 (swapped) operands. These functions are only called if the left operand does
2058 not support the corresponding operation and the operands are of different 2064 not support the corresponding operation [#]_ and the operands are of differen t
2059 types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is 2065 types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is
2060 an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)`` 2066 an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)``
2061 is called if ``x.__sub__(y)`` returns *NotImplemented*. 2067 is called if ``x.__sub__(y)`` returns *NotImplemented*.
2062 2068
2063 .. index:: builtin: pow 2069 .. index:: builtin: pow
2064 2070
2065 Note that ternary :func:`pow` will not try calling :meth:`__rpow__` (the 2071 Note that ternary :func:`pow` will not try calling :meth:`__rpow__` (the
2066 coercion rules would become too complicated). 2072 coercion rules would become too complicated).
2067 2073
2068 .. note:: 2074 .. note::
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
2415 await log('exiting context') 2421 await log('exiting context')
2416 2422
2417 .. versionadded:: 3.5 2423 .. versionadded:: 3.5
2418 2424
2419 2425
2420 .. rubric:: Footnotes 2426 .. rubric:: Footnotes
2421 2427
2422 .. [#] It *is* possible in some cases to change an object's type, under certain 2428 .. [#] It *is* possible in some cases to change an object's type, under certain
2423 controlled conditions. It generally isn't a good idea though, since it can 2429 controlled conditions. It generally isn't a good idea though, since it can
2424 lead to some very strange behaviour if it is handled incorrectly. 2430 lead to some very strange behaviour if it is handled incorrectly.
2431
2432 .. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
2433 :meth:`__contains__` methods have special handling for this; others
2434 will still raise a :exc:`TypeError`, but may do so by relying on
2435 the behavior that ``None`` is not callable.
2436
2437 .. [#] "Does not support" here means that the class has no such method, or
2438 the method returns ``NotImplemented``. Do not set the method to
2439 ``None`` if you want to force fallback to the right operand's reflected
2440 method--that will instead have the opposite effect of explicitly
2441 *blocking* such fallback.
2425 2442
2426 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod 2443 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod
2427 (such as :meth:`__add__`) fails the operation is not supported, which is why the 2444 (such as :meth:`__add__`) fails the operation is not supported, which is why the
2428 reflected method is not called. 2445 reflected method is not called.
OLDNEW
« no previous file with comments | « Doc/library/collections.abc.rst ('k') | Lib/_collections_abc.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+