[Numpy-discussion] What's the difference between calling __mul__ and *?

Will Lee lee.will at gmail.com
Fri Jun 7 12:30:18 EDT 2013


Can somebody tell me why these operations are not the same in numpy?

In [2]: a = numpy.array([1, 2, 3.])

In [4]: matrix = numpy.matrix([[1, 2, 3.], [4, 5, 6], [7, 8, 9]])

In [5]: a.__mul__(matrix)

matrix([[  1.,   4.,   9.],

        [  4.,  10.,  18.],

        [  7.,  16.,  27.]])

In [6]: a * matrix

matrix([[ 30.,  36.,  42.]])


Essentially I'm trying to extend from numpy.ndarray.  From my subclass
__mul__ I'd like to call the parent's __mul__ method.  I ran into
problem when I'm trying to call super(SubArrayClass, self).__mul__()
method when working with a matrix.  I also can't think of a way to use
operator.mul() due to the subclass nature.  Is there any way to make
this work?

Any help is greatly appreciated.

Thanks,

Will



More information about the NumPy-Discussion mailing list