我正在学习Cython,现在正在尝试它。我尝试了基本的cdef类示例程序,它工作得很好。在
现在我要做的是在cdef类类型中混合使用cdef和非cdef属性,如下所示
cdef class Context:
cdef public int byteIndex, bitIndex
def __init__(self, msg = "", msg_len = 0):
self.msg = msg
self.msg_len = msg_len
self.byteIndex = 0
self.bitIndex = 7
但一旦我实例化了对象,我就得到了错误
!! AttributeError: 'c_asnbase.Context' object has no attribute 'msg'
这是否意味着一旦定义了一个带有cdef的python类,所有self.*属性都必须是cdef定义的?在
正如@DavidW所指出的,cdef类的问题是它们没有
__dict__
。如果确实需要,可以将__dict__
添加到类定义中:现在:
^{pr2}$注意:如果
cdef class A
没有__dict__
,而是用cdef public object question
来定义,则setattr(a,'new_attr', None)
是不可能的。在显然,使用
__dict__
会有额外的成本,因此当性能重要时,可能会使用预定义的属性。在另一种方法是创建
cdef class
的子类并使用它而不是基类。在注意:here is the part在关于动态属性的Cython文档中。在
是的。这在the documentation中有明确的表述:
通过将属性定义为object类型,可以很好地存储字符串:
在内部,这是因为
cdef class
没有字典,这样可以节省空间并加快属性访问速度,但这确实意味着它不能在运行时添加任意属性。这相当类似于在普通Python类中使用__slots__
。在相关问题 更多 >
编程相关推荐