簡體   English   中英

為什么不能從 np.ndarray 的實例調用 __array_function__?

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

我正在嘗試按照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

即使正常方法顯然有效

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

function np.broacast_to只是一個例子, np.reshape也有同樣的問題。

有誰知道我的錯誤是什么?

types只應該包含實際實現__array_function__的類型。 type(shape)沒有實現__array_function__ ,所以它不應該在types中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM