[Python-Dev] binary operation heuristics -- bug or undocumented? (original) (raw)

Michael Foord fuzzyman at voidspace.org.uk
Fri Mar 19 14:05:20 CET 2010


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.



More information about the Python-Dev mailing list