Is it possible that class object can have a member of class itself?

44 views
Skip to first unread message

진호용

unread,
Sep 18, 2020, 2:12:18 AM9/18/20
to Numba Public Discussion - Public
I am trying to make a class with a dictionary member that its value is class itself.

ex)
```
class A:
  def __init__(self):
    self.dictionary = { }  # key is integer, value is class A itself
```

Is it possible?

Code below is my try.
```
import numpy as np
import numba
from numba import jit, jitclass, types, typed

dict_type = numba.deferred_type()
node_spec = [
    ('key', numba.uint32),
    ('data', numba.uint32[:]),
    ('count', numba.uint32),
    ('children', dict_type)
]
@numba.experimental.jitclass(node_spec)
class Node:
    def __init__(self, key, data=None):
        self.key = key
        self.data = data
        self.count = 0
        self.children = {}

node_instance = Node.class_type.instance_type
dict_type.define(numba.typeof(typed.Dict.empty(
    key_type=numba.uint32,
    value_type=node_instance
)))
```

Siu Kwan Lam

unread,
Sep 18, 2020, 12:09:40 PM9/18/20
to Numba Public Discussion - Public
The error is from 
```
typed.Dict.empty(
    key_type=numba.uint32,
    value_type=node_instance
))
```
because it is creating a Dict instance but `node_instance` is still not fully defined.

Spell the type with `types.DictType(numba.uint32, node_instance)` instead.
So the last line looks like:
```
dict_type.define(types.DictType(numba.uint32, node_instance))
```

Btw, I recommend using the Numba Discourse forum (https://numba.discourse.group/) for future questions and discussions.

-Siu

--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/cac05c12-bdb0-4e7c-a5d8-17416eb32aabn%40continuum.io.
Reply all
Reply to author
Forward
0 new messages