Skip to content

Why does __array_priority__ not take precedence over __array__? #3164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
astrofrog opened this issue Mar 22, 2013 · 4 comments
Closed

Why does __array_priority__ not take precedence over __array__? #3164

astrofrog opened this issue Mar 22, 2013 · 4 comments

Comments

@astrofrog
Copy link
Contributor

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

[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?

@charris
Copy link
Member

charris commented Feb 21, 2014

Probably oversight, or, never properly thought out.

@seberg
Copy link
Member

seberg commented Feb 21, 2014

This has been fixed not too long ago. The operators now actually do check array priority before calling into the ufunc machinery.

@seberg seberg closed this as completed Feb 21, 2014
@seberg
Copy link
Member

seberg commented Feb 21, 2014

(The corresponding pull request is gh-3501)

@charris
Copy link
Member

charris commented Feb 21, 2014

@seberg Hey, thanks for catching that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants