If I run the following dummy example:
import numpy as np
class MyCustomClass(object):
__array_priority__ = 1000
def __array__(self):
return np.array([1,2,3])
def __mul__(self, other):
return np.array([4,5,6])
def __rmul__(self, other):
return self.__mul__(other)
c = MyCustomClass()
print(np.array([1]) * c)
print(c * np.array([1]))
I get
which in my view is incorrect (__array__ should not get called because __array_priority__ is higher than that for np.array([1])). If I remove the __array__ method, I get the expected result:
as expected. This suggests to me that the presence of __array__ takes precedence over the __array_priority__. Is there a reason for this, or is it an oversight?
Most importantly, is there a way to get around this, and ensure that if __array__ is present, __rmul__ still gets called?
If I run the following dummy example:
I get
which in my view is incorrect (
__array__should not get called because__array_priority__is higher than that fornp.array([1])). If I remove the__array__method, I get the expected result:as expected. This suggests to me that the presence of
__array__takes precedence over the__array_priority__. Is there a reason for this, or is it an oversight?Most importantly, is there a way to get around this, and ensure that if
__array__is present,__rmul__still gets called?