Error when trying to redefine the division operator with __div__
<p>Why does numpy throw an exception for the following code? <code>foo/3</code> works fine.</p>

<pre><code>import numpy as np

class item(object):
    def __init__(self, val = 0.0):
        self._val = val
    @property
    def value(self):
        return self._val
    def __add__(self, other):
        return item(self.value + other.value)
    def __sub__(self, other):
        return item(self.value - other.value)
    def __mul__(self, other):
        if isinstance(other, item):
            return self.value * other.value
        if isinstance(other, (int, float, long)):
            return item(other*self.value)
        raise TypeError("unsupported operand")
    def __div__(self, other):
        if isinstance(other, (int, float, long)):
            return item(self.value/other)
        raise TypeError("unsupported operand")
    def __cmp__(self, other):
        if self.value &lt; other.value:
            return -1
        if self.value == other.value:
            return 0
        return 1
    def __str__(self):
        return "item &lt;%f&gt;" % self.value


data = [ item(x) for x in np.random.random(size = 1000) ]

foo = item(3.1)

foo/3

np.mean(data)
</code></pre>

<p>error message:</p>

<pre><code>     45 foo/3
     46 
---&gt; 47 np.mean(data)
     48 

/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in mean(a, axis, dtype, out, keepdims)    2714     2715     return
_methods._mean(a, axis=axis, dtype=dtype,
-&gt; 2716                             out=out, keepdims=keepdims)    2717     2718 def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False):

/usr/lib/python2.7/dist-packages/numpy/core/_methods.pyc in _mean(a, axis, dtype, out, keepdims)
     67         ret = ret.dtype.type(ret / rcount)
     68     else:
---&gt; 69         ret = ret / rcount
     70 
     71     return ret

TypeError: unsupported operand type(s) for /: 'item' and 'int'
</code></pre>

<p>I need to define <code>__truediv__</code>. The documentation says: </p>

<blockquote>
  <p>object.<code>__div__</code>(self, other) </p>
  
  <p>object.<code>__truediv__</code>(self, other) </p>
  
  <p>The division operator (/) is implemented by these methods. The
  <code>__truediv__()</code> method is used when <code>__future__.division</code> is in effect, otherwise <code>__div__()</code> is used. If only one of these two methods is
  defined, the object will not support division in the alternate
  context; TypeError will be raised instead.</p>
</blockquote>

<p>see <a href="https://docs.python.org/2.7/reference/datamodel.html?highlight=__len__#object.__truediv__" rel="nofollow">https://docs.python.org/2.7/reference/datamodel.html?highlight=<strong>len</strong>#object.<strong>truediv</strong></a></p>

python python-2.7 numpy