首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含自定义Numba对象列表的numba类。

包含自定义Numba对象列表的numba类。
EN

Stack Overflow用户
提问于 2020-03-10 07:23:27
回答 1查看 545关注 0票数 0

我希望B类包含如下A类对象的列表:

代码语言:javascript
复制
from numba import int8, jitclass, types, typed


@jitclass([("field", int8)])
class A:
    def __init__(self):
        self.field = 1


@jitclass([("container", types.ListType(A))])
class B:
    def __init__(self):
        self.container = typed.List(A())


if __name__ == "__main__":

    b = B()

错误:

代码语言:javascript
复制
Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<class 'numba.types.containers.ListType'>) with argument(s) of type(s): (instance.jitclass.A#7f994ec4a860<field:int8>)
 * parameterized
In definition 0:
    TypeError: typer() takes 0 positional arguments but 1 was given
    raised from /home/xxx/miniconda3/envs/yyy/lib/python3.7/site-packages/numba/typing/templates.py:283
In definition 1:
    TypeError: typer() takes 0 positional arguments but 1 was given
    raised from /home/xxx/miniconda3/envs/yyy/lib/python3.7/site-packages/numba/typing/templates.py:283
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: typeref[<class 'numba.types.containers.ListType'>]

我哪里出错了.?还是这个功能不受支持?Numba 0.48,Python 3.7

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-10 14:27:41

我能够通过使用typeof方法来实现这一点。但是,首先我必须创建A列表的实例(我不确定是否有可能避免这种情况)。

代码语言:javascript
复制
from numba import int8, jitclass, types, typed, typeof


@jitclass([("field", int8)])
class A:
    def __init__(self):
        self.field = 1

list_instance = typed.List()
list_instance.append(A())

@jitclass([("container", typeof(list_instance))])        
class B:
    def __init__(self, container):
        self.container = container
代码语言:javascript
复制
list_a = typed.List()
list_a.append(A())
list_a.append(A())
b = B(list_a)
print(b)
print(b.container)
print(b.container[0].field)

输出:

代码语言:javascript
复制
<numba.jitclass.boxing.B object at 0x0000020BF396C0B0>
[<numba.jitclass.boxing.A object at 0x0000020BF385AEB0>, <numba.jitclass.boxing.A object at 0x0000020BF385A810>]
1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60613069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档