Python和Numba在Jitclass和Deferred类型上有不同的行为

时间:2019-02-06 15:50:04

标签: python jit numba

我将以下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>

注意:

  1. 它们并不完全相同(我希望它们与仅使用Python的示例一样完全相同)
  2. n等于self,而n.left不等于self.left
  3. 只有n.right等于self.right,但它们都应该彼此相等。

0 个答案:

没有答案