简体   繁体   English

为什么不能从 np.ndarray 的实例调用 __array_function__?

[英]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 .我正在尝试按照numpy 的指南编写自定义数组容器,但我不明白为什么以下代码总是返回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. function np.broacast_to只是一个例子, np.reshape也有同样的问题。

Does anyone know what my mistake is?有谁知道我的错误是什么?

types is only supposed to include types that actually implement __array_function__ . types只应该包含实际实现__array_function__的类型。 type(shape) doesn't implement __array_function__ , so it shouldn't be in types . type(shape)没有实现__array_function__ ,所以它不应该在types中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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