This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: __radd__(self,other) isn't called if self and other are of the same class
Type: Stage:
Components: Versions: Python 3.0, Python 2.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Frosburn, amaury.forgeotdarc, mishok13
Priority: normal Keywords:

Created on 2008-06-23 13:01 by Frosburn, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg68625 - (view) Author: Pyry Pakkanen (Frosburn) Date: 2008-06-23 13:01
>>> class test: 
... 	def __radd__(self,other):
... 		return '__radd__ called'
>>> t = test()
>>> 1 + t
'__radd__ called'
>>> t + t
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'test' and 'test'

This applies to all of the reflected operations.
msg68626 - (view) Author: Andrii V. Mishkovskyi (mishok13) Date: 2008-06-23 13:36
This is covered in section 3.4.7 of Python Reference Manual:
__radd__(self, other)
    These methods are called to implement the binary arithmetic
operations (+, -, *, /, %, divmod(), pow(), **, <<, >>, &, ^, |) with
reflected (swapped) operands. These functions are only called if the
left operand does not support the corresponding operation and the
operands are of different types.[3.3] For instance, to evaluate the
expression x-y, where y is an instance of a class that has an __rsub__()
method, y.__rsub__(x) is called if x.__sub__(y) returns NotImplemented. 
[3.3] For operands of the same type, it is assumed that if the
non-reflected method (such as __add__()) fails the operation is not
supported, which is why the reflected method is not called.
msg68628 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-06-23 14:21
As Andrii said, this behaviour is by design.
Date User Action Args
2022-04-11 14:56:35adminsetgithub: 47428
2008-06-23 14:21:20amaury.forgeotdarcsetstatus: open -> closed
resolution: not a bug
messages: + msg68628
nosy: + amaury.forgeotdarc
2008-06-23 13:37:04mishok13setnosy: + mishok13
messages: + msg68626
2008-06-23 13:01:17Frosburncreate