问题描述
我有:
cdef class BaseClass():
def __cinit__(self,char* name):
print "BaseClass __cinit__()"
#...
def __dealloc__():
print "BaseClass __dealloc__()"
#...
cdef class DerClass(BaseClass):
def __cinit__(self,char* name,int n):
print "DerClass __cinit__()"
#...
def __dealloc__():
print "DerClass __dealloc__()"
#...
当我在cyhton中调用DerClass时,会自动调用BaseClass的构造函数,它的打印内容是:
when i call the DerClass in cyhton happen that the construcor of the BaseClass is called automatically,what it has to print is:
BaseClass __cinit__()
DerClass __cinit__()
DerClass __dealloc__()
BaseClass __dealloc__()
却没有,它使我称之为DerClass('Cia o’)。
为什么会发生这种情况,以及如何避免调用BaseClass的 cinit 。
谢谢!
but it does not,it crash ones that i call the DerClass('Ciao').why does happen so and how can i avoid calling the cinit of BaseClass.Thank you!
推荐答案
上面的答案可能并不是最好的解决方案。阅读上面链接的初始化方法:__cinit __()和__init __()部分将给出以下信息:
The above answer may not pose quite the best solution. Reading the section "Initialisation methods: __cinit__() and __init__()" of the link above gives this information:
和
所以我的解决方案是只替换 __ cinit()__
在 BaseClass
中,以便可以将可变数量的参数传递给任何派生类:
So my solution would be to just replace the arguments of __cinit()__
in BaseClass
so that a variable number of arguments can be passed to any derived class:
cdef class BaseClass:
def __cinit__(self, *argv):
print "BaseClass __cinit__()"
#...
def __dealloc__(self):
print "BaseClass __dealloc__()"
#...
cdef class DerClass(BaseClass):
def __cinit__(self,char* name, int n):
print "DerClass __cinit__()"
#...
def __dealloc__(self):
print "DerClass __dealloc__()"
#...
请参见,以解释python中的 * args
变量
See here for an explanation of the *args
variable in python
这篇关于Cython使用cinit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!