<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Thank you. I tried __rtruediv__ and it works.<div><br><div><div>On Jan 23, 2012, at 12:14 AM, Ian Kelly wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div class="gmail_quote">On Sun, Jan 22, 2012 at 10:22 PM, Massimo Di Pierro <span dir="ltr"><<a href="mailto:massimo.dipierro@gmail.com">massimo.dipierro@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hello everybody,<br>
<br>
I hope somebody could help me with this problem. If this is not the right place to ask, please direct me to the right place and apologies.<br>
I am using Python 2.7 and I am writing some code I want to work on 3.x as well. The problem can be reproduced with this code:<br>
<br>
# from __future__ import division<br>
class Number(object):<br>
    def __init__(self,number):<br>
        self.number=number<br>
    def __rdiv__(self,other):<br>
        return other/self.number<br>
print 10/Number(5)<br>
<br>
It prints 2 as I expect. But if I uncomment the first line, I get:<br>
<br>
Traceback (most recent call last):<br>
  File "test.py", line 8, in <module><br>
    print 10/Number(5)<br>
TypeError: unsupported operand type(s) for /: 'int' and 'Number'<br>
<br>
Is this a bug or the __future__ division in 3.x changed the way operators are overloaded? Where can I read more?<br>
<span class="HOEnZb"></span><br></blockquote></div><br>In Python 3, the / operator uses __truediv__ and the // operator uses __floordiv__.<br>In Python 2, the / operator uses __div__, unless the future import is in effect, and then it uses __truediv__ like Python 3.<br>

<br><a href="http://docs.python.org/reference/datamodel.html#emulating-numeric-types">http://docs.python.org/reference/datamodel.html#emulating-numeric-types</a><br><br>Cheers,<br>Ian<br><div style="" id="avg_ls_inline_popup">

</div><style type="text/css">#avg_ls_inline_popup{position: absolute;z-index: 9999;padding: 0px 0px;margin-left: 0px;margin-top: 0px;overflow: hidden;word-wrap: break-word;color: black;font-size: 10px;text-align: left;line-height: 130%;}</style>
</blockquote></div><br></div></body></html>