I see the same behaviour, moreover when I change class Quantity to a classic class (removing '(object)'), it works as expected. (i.e. Quanitity.__add__() is called after the fourth print. I run Python 2.6.2 on Vista.<br>
<br><br><br><br><br><br><div class="gmail_quote">On Sun, Oct 18, 2009 at 7:50 AM, Darren Dale <span dir="ltr"><<a href="mailto:dsdale24@gmail.com">dsdale24@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
According to <a href="http://docs.python.org/reference/datamodel.html" target="_blank">http://docs.python.org/reference/datamodel.html</a> , the<br>
reflected operands functions like __radd__ "are only called if the<br>
left operand does not support the corresponding operation and the<br>
operands are of different types. [3] For instance, to evaluate the<br>
expression x - y, where y is an instance of a class that has an<br>
__rsub__() method, y.__rsub__(x) is called if x.__sub__(y) returns<br>
NotImplemented."<br>
<br>
Consider the following simple example:<br>
<br>
==========================<br>
class Quantity(object):<br>
<br>
def __add__(self, other):<br>
return '__add__ called'<br>
<br>
def __radd__(self, other):<br>
return '__radd__ called'<br>
<br>
class UnitQuantity(Quantity):<br>
<br>
def __add__(self, other):<br>
return '__add__ called'<br>
<br>
def __radd__(self, other):<br>
return '__radd__ called'<br>
<br>
print 'Quantity()+Quantity()', Quantity()+Quantity()<br>
print 'UnitQuantity()+UnitQuantity()', UnitQuantity()+UnitQuantity()<br>
print 'UnitQuantity()+Quantity()', UnitQuantity()+Quantity()<br>
print 'Quantity()+UnitQuantity()', Quantity()+UnitQuantity()<br>
==========================<br>
<br>
The output should indicate that __add__ was called in all four trials,<br>
but the last trial calls __radd__. Interestingly, if I comment out the<br>
definition of __radd__ in UnitQuantity, then the fourth trial calls<br>
__add__ like it should.<br>
<br>
I think this may be an important bug. I'm running Python 2.6.4rc1<br>
(r264rc1:75270, Oct 13 2009, 17:02:06) an ubuntu Karmic. Is it a known<br>
issue, or am I misreading the documentation?<br>
<br>
Thanks,<br>
Darren<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/ehsanamiri%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/ehsanamiri%40gmail.com</a><br>
</blockquote></div><br>