如何在Numba中使用Python类 [英] How to use Python Class with Numba

查看:179
本文介绍了如何在Numba中使用Python类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Numba 0.24,它支持类。

I have Numba 0.24 and it supports classes.

当我尝试构建最简单的类时,我可以想象我发现了一个错误!

When I try to build the simplest class I can imagine I find an error! What's happening?

from numba import jitclass
@jitclass
class foo:
    x = 2
bar = foo()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-3e0fd8d4bd2b> in <module>()
      3 class foo:
      4     x = 2
----> 5 bar = foo()

TypeError: wrap() missing 1 required positional argument: 'cls'

我在这里缺少什么吗?

推荐答案

您需要指定一个规范:

spec = [('x', nb.int64)]
@nb.jitclass(spec)
class foo(object):
    def __init__(self):
        self.x = 2

bar = foo()
print bar.x

看看文档。此时,不支持类变量。您必须使用实例变量。

Take a look at the docs. At this point class variables are not supported. You have to use instance variables.

这篇关于如何在Numba中使用Python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆