我将以下Python代码移植到Numba:
class NODE(object):
def __init__(self):
self.left = self
self.right = self
print(self)
print(self.left)
print(self.right)
if __name__ == "__main__":
n = NODE()
print(n)
print(n.left)
print(n.right)
对于所有六个打印语句,我将同一行打印六次,这是预期的:
<__main__.NODE object at 0x1040aa668>
<__main__.NODE object at 0x1040aa668>
<__main__.NODE object at 0x1040aa668>
<__main__.NODE object at 0x1040aa668>
<__main__.NODE object at 0x1040aa668>
<__main__.NODE object at 0x1040aa668>
但是,将其移植到Numba:
from numba import deferred_type, jitclass
node_type = deferred_type()
node_spec = [("left", node_type),
("right", node_type),
]
@jitclass(node_spec)
class NODE(object):
def __init__(self):
self.left = self
self.right = self
print(self)
print(self.left)
print(self.right)
node_type.define(NODE.class_type.instance_type)
if __name__ == "__main__":
n = NODE()
print(n)
print(n.left)
print(n.right)
我得到以下打印语句:
<numba.jitclass.boxing.NODE object at 0x1101df0f0>
<numba.jitclass.boxing.NODE object at 0x1101df0d0>
<numba.jitclass.boxing.NODE object at 0x1101df0f0>
<numba.jitclass.boxing.NODE object at 0x1101df0d0>
<numba.jitclass.boxing.NODE object at 0x1101df0f0>
<numba.jitclass.boxing.NODE object at 0x1101df0f0>
注意:
n
等于self
,而n.left
不等于self.left
n.right
等于self.right
,但它们都应该彼此相等。