Michael
Michael

Reputation: 1498

Error when trying to redefine the division operator with __div__

Why does numpy throw an exception for the following code? foo/3 works fine.

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 < other.value:
            return -1
        if self.value == other.value:
            return 0
        return 1
    def __str__(self):
        return "item <%f>" % self.value


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

foo = item(3.1)

foo/3

np.mean(data)

error message:

     45 foo/3
     46 
---> 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,
-> 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:
---> 69         ret = ret / rcount
     70 
     71     return ret

TypeError: unsupported operand type(s) for /: 'item' and 'int'

Upvotes: 3

Views: 216

Answers (1)

Michael
Michael

Reputation: 1498

I need to define __truediv__. The documentation says:

object.__div__(self, other)

object.__truediv__(self, other)

The division operator (/) is implemented by these methods. The __truediv__() method is used when __future__.division is in effect, otherwise __div__() 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.

see https://docs.python.org/2.7/reference/datamodel.html?highlight=len#object.truediv

Upvotes: 3

Related Questions