[Numpy-discussion] __array_priority__ ignored if __array__ is present

Thomas Robitaille thomas.robitaille at gmail.com
Thu May 16 09:19:51 EDT 2013


Hi everyone,

(this was posted as part of another topic, but since it was unrelated,
I'm reposting as a separate thread)

I've also been having issues with __array_priority__ - the following
code behaves differently for __mul__ and __rmul__:

"""
import numpy as np


class TestClass(object):

    def __init__(self, input_array):
        self.array = input_array

    def __mul__(self, other):
        print "Called __mul__"

    def __rmul__(self, other):
        print "Called __rmul__"

    def __array_wrap__(self, out_arr, context=None):
        print "Called __array_wrap__"
        return TestClass(out_arr)

    def __array__(self):
        print "Called __array__"
        return np.array(self.array)
"""

with output:

"""
In [7]: a = TestClass([1,2,3])

In [8]: print type(np.array([1,2,3]) * a)
Called __array__
Called __array_wrap__
<class '__main__.TestClass'>

In [9]: print type(a * np.array([1,2,3]))
Called __mul__
<type 'NoneType'>
"""

Is this also an oversight? I opened a ticket for it a little while ago:

https://github.com/numpy/numpy/issues/3164

Any ideas?

Thanks!
Tom



More information about the NumPy-Discussion mailing list