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

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

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 3 years, 6 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 1044 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 1055
1056 A class can implement certain operations that are invoked by special syntax 1056 A class can implement certain operations that are invoked by special syntax
1057 (such as arithmetic operations or subscripting and slicing) by defining methods 1057 (such as arithmetic operations or subscripting and slicing) by defining methods
1058 with special names. This is Python's approach to :dfn:`operator overloading`, 1058 with special names. This is Python's approach to :dfn:`operator overloading`,
1059 allowing classes to define their own behavior with respect to language 1059 allowing classes to define their own behavior with respect to language
1060 operators. For instance, if a class defines a method named :meth:`__getitem__`, 1060 operators. For instance, if a class defines a method named :meth:`__getitem__`,
1061 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent 1061 and ``x`` is an instance of this class, then ``x[i]`` is roughly equivalent
1062 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n 1062 to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a n
1063 operation raise an exception when no appropriate method is defined (typically 1063 operation raise an exception when no appropriate method is defined (typically
1064 :exc:`AttributeError` or :exc:`TypeError`). 1064 :exc:`AttributeError` or :exc:`TypeError`).
1065
1066 Setting a special method to ``None`` indicates that the corresponding
1067 operation is not available. For example, if a class sets
1068 :meth:`__iter__` to ``None``, the class is not iterable, so calling
1069 :func:`iter` on its instances will raise a :exc:`TypeError` (without
1070 falling back to :meth:`__getitem__`). [#]_
1065 1071
1066 When implementing a class that emulates any built-in type, it is important that 1072 When implementing a class that emulates any built-in type, it is important that
1067 the emulation only be implemented to the degree that it makes sense for the 1073 the emulation only be implemented to the degree that it makes sense for the
1068 object being modelled. For example, some sequences may work well with retrieval 1074 object being modelled. For example, some sequences may work well with retrieval
1069 of individual elements, but extracting a slice may not make sense. (One example 1075 of individual elements, but extracting a slice may not make sense. (One example
1070 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document 1076 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document
1071 Object Model.) 1077 Object Model.)
1072 1078
1073 1079
1074 .. _customization: 1080 .. _customization:
(...skipping 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after
2106 object.__ror__(self, other) 2112 object.__ror__(self, other)
2107 2113
2108 .. index:: 2114 .. index::
2109 builtin: divmod 2115 builtin: divmod
2110 builtin: pow 2116 builtin: pow
2111 2117
2112 These methods are called to implement the binary arithmetic operations 2118 These methods are called to implement the binary arithmetic operations
2113 (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`, 2119 (``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
2114 :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected 2120 :func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected
2115 (swapped) operands. These functions are only called if the left operand does 2121 (swapped) operands. These functions are only called if the left operand does
2116 not support the corresponding operation and the operands are of different 2122 not support the corresponding operation [#]_ and the operands are of differen t
2117 types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is 2123 types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is
2118 an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)`` 2124 an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)``
2119 is called if ``x.__sub__(y)`` returns *NotImplemented*. 2125 is called if ``x.__sub__(y)`` returns *NotImplemented*.
2120 2126
2121 .. index:: builtin: pow 2127 .. index:: builtin: pow
2122 2128
2123 Note that ternary :func:`pow` will not try calling :meth:`__rpow__` (the 2129 Note that ternary :func:`pow` will not try calling :meth:`__rpow__` (the
2124 coercion rules would become too complicated). 2130 coercion rules would become too complicated).
2125 2131
2126 .. note:: 2132 .. note::
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
2521 await log('exiting context') 2527 await log('exiting context')
2522 2528
2523 .. versionadded:: 3.5 2529 .. versionadded:: 3.5
2524 2530
2525 2531
2526 .. rubric:: Footnotes 2532 .. rubric:: Footnotes
2527 2533
2528 .. [#] It *is* possible in some cases to change an object's type, under certain 2534 .. [#] It *is* possible in some cases to change an object's type, under certain
2529 controlled conditions. It generally isn't a good idea though, since it can 2535 controlled conditions. It generally isn't a good idea though, since it can
2530 lead to some very strange behaviour if it is handled incorrectly. 2536 lead to some very strange behaviour if it is handled incorrectly.
2537
2538 .. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
2539 :meth:`__contains__` methods have special handling for this; others
2540 will still raise a :exc:`TypeError`, but may do so by relying on
2541 the behavior that ``None`` is not callable.
2542
2543 .. [#] "Does not support" here means that the class has no such method, or
2544 the method returns ``NotImplemented``. Do not set the method to
2545 ``None`` if you want to force fallback to the right operand's reflected
2546 method--that will instead have the opposite effect of explicitly
2547 *blocking* such fallback.
2531 2548
2532 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod 2549 .. [#] For operands of the same type, it is assumed that if the non-reflected me thod
2533 (such as :meth:`__add__`) fails the operation is not supported, which is why the 2550 (such as :meth:`__add__`) fails the operation is not supported, which is why the
2534 reflected method is not called. 2551 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+