@@ -3334,12 +3334,13 @@ left undefined.
3334
3334
These methods are called to implement the binary arithmetic operations
3335
3335
(``+ ``, ``- ``, ``* ``, ``@ ``, ``/ ``, ``// ``, ``% ``, :func: `divmod `,
3336
3336
:func: `pow `, ``** ``, ``<< ``, ``>> ``, ``& ``, ``^ ``, ``| ``) with reflected
3337
- (swapped) operands. These functions are only called if the left operand does
3338
- not support the corresponding operation [# ]_ and the operands are of different
3339
- types. [# ]_ For instance, to evaluate the expression ``x - y ``, where *y * is
3340
- an instance of a class that has an :meth: `__rsub__ ` method,
3341
- ``type(y).__rsub__(y, x) `` is called if ``type(x).__sub__(x, y) `` returns
3342
- :data: `NotImplemented `.
3337
+ (swapped) operands. These functions are only called if the operands
3338
+ are of different types, when the left operand does not support the corresponding
3339
+ operation [# ]_, or the right operand's class is derived from the left operand's
3340
+ class. [# ]_ For instance, to evaluate the expression ``x - y ``, where *y * is
3341
+ an instance of a class that has an :meth: `__rsub__ ` method, ``type(y).__rsub__(y, x) ``
3342
+ is called if ``type(x).__sub__(x, y) `` returns :data: `NotImplemented ` or ``type(y) ``
3343
+ is a subclass of ``type(x) ``. [# ]_
3343
3344
3344
3345
.. index :: pair: built-in function; pow
3345
3346
@@ -3354,7 +3355,6 @@ left undefined.
3354
3355
non-reflected method. This behavior allows subclasses to override their
3355
3356
ancestors' operations.
3356
3357
3357
-
3358
3358
.. method :: object.__iadd__(self, other)
3359
3359
object.__isub__(self, other)
3360
3360
object.__imul__(self, other)
@@ -3881,7 +3881,10 @@ An example of an asynchronous context manager class::
3881
3881
method—that will instead have the opposite effect of explicitly
3882
3882
*blocking * such fallback.
3883
3883
3884
- .. [# ] For operands of the same type, it is assumed that if the non-reflected
3885
- method -- such as :meth: `~object.__add__ ` -- fails then the overall
3886
- operation is not
3887
- supported, which is why the reflected method is not called.
3884
+ .. [# ] For operands of the same type, it is assumed that if the non-reflected method
3885
+ (such as :meth: `~object.__add__ `) fails then the operation is not supported, which is why the
3886
+ reflected method is not called.
3887
+
3888
+ .. [# ] If the right operand's type is a subclass of the left operand's type, the
3889
+ reflected method having precedence allows subclasses to override their ancestors'
3890
+ operations.
0 commit comments