Python类可以具有类属性:
class Foo(object):
bar = 4
Run Code Online (Sandbox Code Playgroud)
是否有类似的结构用于在Cython扩展类型中定义类属性?例如,当我尝试编译以下cython代码时
cdef class Foo:
cdef int bar
bar = 4
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
thing.c:773:3: error: use of undeclared identifier 'bar'
bar = 4;
^
1 error generated.
error: command 'cc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud) 我正在与 Cython 合作,为一个大学项目优化我的 Python 代码。为此,我想将 python 类转换为扩展类型。我目前在编译一种扩展类型时遇到问题,该扩展类型应该是另一种扩展类型的子类。这是我得到的错误:
src/core/ast/ast_classes/AstPreprocessor.pyx:9:27: First base of 'AstPreprocessor' is not an extension type
Run Code Online (Sandbox Code Playgroud)
AstPreprocessor的定义如下:
#Edit
from src.core.ast.ast_classes.AstBase import AstBase
cdef class AstPreprocessor(AstBase):
cdef str function_name
def __init__(self, function_ast, str function_name):
super().__init__(function_ast)
self.ast.index = self.ast.index.map(str)
self.function_name = function_name
self.symbol_list = super().get_symbol_list(self.function_name)
#more method declarations
Run Code Online (Sandbox Code Playgroud)
这是 AstBase 类的一部分,包括 中调用的方法AstPreprocessor#__init__():
cdef class AstBase:
cdef int length
def __init__(self, df):
self.ast = df
self.length = int(df.shape[0])
self.childrens = {}
#more method declarations
cdef get_symbol_list(self, str function_name): …Run Code Online (Sandbox Code Playgroud)