[Python-Dev] binary operation heuristics -- bug or undocumented?
fuzzyman at voidspace.org.uk
Fri Mar 19 14:05:20 CET 2010
On 19/03/2010 12:46, Alex A. Naanou wrote:
>>>> 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,
> 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
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.
More information about the Python-Dev