binary operation heuristics -- bug or undocumented?

Hi, A friend of mine stumbled upon the following behavior: ---cut---
class A(object): pass ... class B(object): ... def __add__(self, other): ... print 'B: adding B and %s objects.' % other.__class__.__name__ ... class C(object): ... def __radd__(self, other): ... print 'C: adding C and %s objects.' % other.__class__.__name__ ... a, b, c = A(), B(), C()
b + c B: adding B and C objects.
a + c C: adding C and A objects.
# so far, quite logical. now let's do this:
1 + c C: adding C and int objects.
--uncut-- My first expectation would be to get a TypeError here, as ints indeed have an __add__ method, and they do not know anything about C objects (obviously :) ). On second thought, giving client code priority to handle things has it's merits. The problem is that I found no mention of this behavior in the docs. P.S. tested in 2.5 through 3.0 and PyPy Thanks. -- Alex.

On Fri, Mar 19, 2010 at 12:46 PM, Alex A. Naanou <alex.nanou@gmail.com> wrote:
A friend of mine stumbled upon the following behavior:
---cut---
class A(object): pass ... class B(object): ... def __add__(self, other): ... print 'B: adding B and %s objects.' % other.__class__.__name__ ... class C(object): ... def __radd__(self, other): ... print 'C: adding C and %s objects.' % other.__class__.__name__ ... a, b, c = A(), B(), C()
b + c B: adding B and C objects.
a + c C: adding C and A objects.
# so far, quite logical. now let's do this:
1 + c C: adding C and int objects.
--uncut--
My first expectation would be to get a TypeError here, as ints indeed have an __add__ method, and they do not know anything about C objects (obviously :) ).
Yes: the int.__add__ method is tried first. Since it doesn't know anything about C objects it returns NotImplemented, and then C.__radd__ is given a chance to do the addition. The rules are documented here: http://docs.python.org/reference/datamodel.html#coercion-rules Mark

On 19/03/2010 12:46, Alex A. Naanou wrote:
[snip...]
class C(object):
... def __radd__(self, other): ... print 'C: adding C and %s objects.' % other.__class__.__name__ ...
a, b, c = A(), B(), C() 1 + c
C: adding C and int objects.
That is the whole point of the __radd__ method. ints don't know how to add themselves to C objects, so int.__add__ will return NotImplemented and then Python will try C.__radd__. All the best, Michael
--uncut--
My first expectation would be to get a TypeError here, as ints indeed have an __add__ method, and they do not know anything about C objects (obviously :) ). On second thought, giving client code priority to handle things has it's merits.
The problem is that I found no mention of this behavior in the docs.
P.S. tested in 2.5 through 3.0 and PyPy
Thanks.
-- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

Thanks! On Fri, Mar 19, 2010 at 16:05, Michael Foord <fuzzyman@voidspace.org.uk> wrote:
On 19/03/2010 12:46, Alex A. Naanou wrote:
[snip...]
class C(object):
... def __radd__(self, other): ... print 'C: adding C and %s objects.' % other.__class__.__name__ ...
a, b, c = A(), B(), C() 1 + c
C: adding C and int objects.
That is the whole point of the __radd__ method. ints don't know how to add themselves to C objects, so int.__add__ will return NotImplemented and then Python will try C.__radd__.
All the best,
Michael
--uncut--
My first expectation would be to get a TypeError here, as ints indeed have an __add__ method, and they do not know anything about C objects (obviously :) ). On second thought, giving client code priority to handle things has it's merits.
The problem is that I found no mention of this behavior in the docs.
P.S. tested in 2.5 through 3.0 and PyPy
Thanks.
-- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog
READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.
P.S. like the licence :)
-- Alex.

On Fri, Mar 19, 2010 at 03:46:07PM +0300, Alex A. Naanou wrote:
class C(object): ... def __radd__(self, other): ... print 'C: adding C and %s objects.' % other.__class__.__name__ ... 1 + c C: adding C and int objects.
My first expectation would be to get a TypeError here, as ints indeed have an __add__ method, and they do not know anything about C objects (obviously :) ). On second thought, giving client code priority to handle things has it's merits.
The problem is that I found no mention of this behavior in the docs.
It's well-known and documented behavior. It's what r-methods are for. See http://docs.python.org/reference/datamodel.html#emulating-numeric-types "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] 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." Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.
participants (5)
-
Alex A. Naanou
-
Mark Dickinson
-
Michael Foord
-
Oleg Broytman
-
Terry Reedy