简体   繁体   中英

Why can't __array_function__ be called from an instance of an np.ndarray?

I am trying to write a custom array container following numpy's guide and I can't understand why the following code always returns NotImplemented .

import numpy as np
a = np.array([1])
shape = (2, 2)
a.__array_function__(func=np.broadcast_to, types=(np.ndarray, type(shape)), args=(a, shape), kwargs={})
Out[5]: NotImplemented

Even though the normal method obviously works

np.broadcast_to(a, shape)
Out[6]: 
array([[1, 1],
       [1, 1]])

The function np.broacast_to is just an example, np.reshape has the same issue.

Does anyone know what my mistake is?

types is only supposed to include types that actually implement __array_function__ . type(shape) doesn't implement __array_function__ , so it shouldn't be in types .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM