You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
[1 2 3]
[4 5 6]
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:
[4 5 6]
[4 5 6]
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?
The text was updated successfully, but these errors were encountered:
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?The text was updated successfully, but these errors were encountered: