使用cython cdef类和super超出了递归限制

时间:2014-02-22 19:51:30

标签: cython

我在使用Cython 2.0运行的这段代码时遇到了一些麻烦:

cdef class Foo(object):
  cpdef twerk(self): #using def instead does not help
    print "Bustin' some awkward moves."

cdef class ShyFoo(Foo):
  cpdef twerk(self):
    print "Do I really have to?"
    super(self, ShyFoo).twerk()
    print "I hate you so much."

ShyFoo().twerk()
  

RuntimeError:调用Python对象时超出了最大递归深度

但是,删除cdef并用cpdef替换def s会让我使用Python。

回溯看起来像这样:

File "mytest.pyx", line 61, in mytest.Foo.twerk
  cpdef twerk(self):
File "mytest.pyx", line 67, in mytest.ShyFoo.twerk
  super(ShyFoo, self).twerk()
File "mytest.pyx", line 61, in mytest.Foo.twerk
  cpdef twerk(self):
File "mytest.pyx", line 67, in mytest.ShyFoo.twerk
    super(ShyFoo, self).twerk()
....................................................

我做错了什么?我从4年前发现了this relevant ticket,但我想由于用户错误而没有引起注意。

1 个答案:

答案 0 :(得分:1)

您问题中链接的错误似乎就是问题所在。如果你将两个方法都改为def而不是cpdef,那么它可以正常工作。或者,你可以删除对super的调用:

cdef class Foo(object):
    cpdef twerk(self):
        print "Bustin' some awkward moves."

cdef class ShyFoo(Foo):
    cpdef twerk(self):
        print "Do I really have to?"
        Foo.twerk(self)
        print "I hate you so much."

ShyFoo().twerk()
相关问题