不要用Cython扩展类型支持类的属性? [英] Do Cython extension types support class attributes?
问题描述
Python类可以有类属性:
类Foo(对象):
巴= 4
有没有在用Cython扩展类型定义类的属性类似的结构?例如,当我尝试编译下面用Cython code
CDEF类Foo:
CDEF INT酒吧
巴= 4
我得到这个错误:
thing.c:773:3:错误:使用未声明的标识符酒吧的
巴= 4;
^
产生1个错误。
错误:命令'CC',退出状态1失败
虽然它似乎并不可能有C-类型的静态属性,用Cython扩展类型可以有这也是在Python自动访问正规Python静态属性。刚刚宣布他们为你将宣布他们的Python:
CDEF类Foo:
巴= 4
生成的code表明,如果你在使用C-类型的上下文中使用它们这些静态属性被存储在类对象,即属性字典Python对象,它们是从Python对象转换回。
Python classes can have class attributes:
class Foo(object):
bar = 4
Is there an analogous construct for defining class attributes in Cython extension types? For example, when I try to compile the following cython code
cdef class Foo:
cdef int bar
bar = 4
I get this error:
thing.c:773:3: error: use of undeclared identifier 'bar'
bar = 4;
^
1 error generated.
error: command 'cc' failed with exit status 1
While it doesn't seem to be possible to have C-typed static attributes, Cython extension types can have regular Python static attributes which are also automatically accessible in Python. Just declare them as you would declare them in Python:
cdef class Foo:
bar = 4
The generated code shows that these static attributes are stored as Python objects in the attribute dict of the class object, i.e. if you use them in contexts where C-types are used, they are converted back from Python objects.
这篇关于不要用Cython扩展类型支持类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!