Cython扩展类型支持类属性吗?

2024-04-26 00:08:10 发布

您现在位置:Python中文网/ 问答频道 /正文

Python类可以具有类属性:

class Foo(object):
   bar = 4

在Cython扩展类型中是否有类似的构造来定义类属性?例如,当我试图编译以下cython代码时

cdef class Foo:
    cdef int bar
    bar = 4

我得到这个错误:

thing.c:773:3: error: use of undeclared identifier 'bar'
  bar = 4;
  ^
1 error generated.
error: command 'cc' failed with exit status 1

Tags: 代码类型属性定义objectfoo错误bar