Класс Numba, содержащий список пользовательских объектов Numba - PullRequest
0 голосов
/ 10 марта 2020

Я бы хотел, чтобы класс B содержал список объектов A следующим образом:

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()

Ошибка:

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'>]

Где я ошибся ...? Или эта функциональность не поддерживается? Нумба 0,48, Python 3,7

1 Ответ

1 голос
/ 10 марта 2020

Мне удалось добиться этого с помощью метода typeof. Однако сначала я должен был создать экземпляр списка A (я не уверен, можно ли этого избежать).

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
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)

Вывод:

<numba.jitclass.boxing.B object at 0x0000020BF396C0B0>
[<numba.jitclass.boxing.A object at 0x0000020BF385AEB0>, <numba.jitclass.boxing.A object at 0x0000020BF385A810>]
1
...