【问题标题】:Numba - TypeError: 'type' object has no attribute '__getitem__Numba - TypeError:'type'对象没有属性'__getitem__
【发布时间】:2023-09-05 15:25:02
【问题描述】:

我在 Anaconda 中使用 Numba,不知道为什么

@jit(argtypes=[int16[:], int16[:]])
def index(ic, nc):
    return ic[0] + nc[0] * (ic[1] + nc[1] * ic[2])

不起作用:

TypeError: 'type' object has no attribute '__getitem__'

但是如果我使用@autojit 而不是@jit(..) 一切都很好。

【问题讨论】:

    标签: typeerror jit numba


    【解决方案1】:

    阅读 Numba 示例有点让人困惑,但您实际上需要从 numba 命名空间导入 int16

    您看到的错误与从 NumPy 导入 int16 一致。因此,如果在文件的顶部,您的代码如下所示:

    from numba import *
    from numpy import *
    

    然后你会不小心用它的 NumPy 定义覆盖 int16。有两个修复。首先,您可以交换导入的顺序:

    from numpy import *
    from numba import *
    

    或者,更准确地说,您可以导入不带 * 的命名空间并明确引用您需要的内容:

    import numba as nb
    
    @nb.jit(argtypes=[nb.int16[:], nb.int16[:]])
    

    【讨论】: