【问题标题】:Declaring variables in numba jitclass在 numba jitclass 中声明变量
【发布时间】:2016-11-09 21:52:41
【问题描述】:

当我尝试导入以下 jitclass 时,我的字典出现 KeyError:

from numba import jitclass, float64

spec = [('n', float64),
        ('w', float64),
        ('a', float64)]

@jitclass(spec)
class foo:

    def __init__(self,n,w):

        self.n = n
        self.w = w

    def foo2(self):

        a = self.n*self.w

        return a + 1.

我不明白错误来自哪里。 numba 文档不足以解释事情。我需要所有变量都是类变量吗?

【问题讨论】:

  • 您的班级没有a 字段。为什么要在规范中列出一个?
  • 如果我将“a”条目从规范中拉出,我会得到同样的错误,所以这实际上似乎不是问题。或者它是以一种非常迟钝的方式。

标签: python numba


【解决方案1】:

jitclass 需要继承自object:

from numba import jitclass, float64

spec = [('n', float64),
        ('w', float64),
        ('a', float64)]

@jitclass(spec)
class foo(object):

    def __init__(self,n,w):

        self.n = n
        self.w = w

    def foo2(self):

        a = self.n*self.w

        return a + 1.

这似乎没有在文档中作为要求提及,但它是示例的编写方式。

【讨论】:

  • 谢谢!这将是在文档中提及的一件非常重要的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
  • 2020-08-21
  • 1970-01-01
  • 2022-10-04
  • 2019-06-07
  • 1970-01-01
相关资源
最近更新 更多