【发布时间】:2020-07-21 15:40:44
【问题描述】:
我正在与 Cython 合作,为一个大学项目优化我的 Python 代码。 为此,我想将 python 类转换为扩展类型。我目前在编译一种扩展类型时遇到问题,应该是另一种扩展类型的子类。 这是我得到的错误:
src/core/ast/ast_classes/AstPreprocessor.pyx:9:27: First base of 'AstPreprocessor' is not an extension type
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
这是 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):
symbol_list = []
for i in self.ast.index:
i = int(i)
if self.token(i).startswith('SYMBOL') \
and self.text(i) != function_name:
symbol_list.append(i)
return symbol_list
这是我的 setup.py 中的 cythonize 命令:
ext_modules=cythonize(["src/core/ast/ast_classes/*.pyx",
"src/core/ast/preprocessing/*.pyx"],
language_level=3, annotate=True),
我查看了文档,但我很难真正理解为什么会发生此错误以及如何修复它。这是我第一次使用 Cython,因此将不胜感激。
编辑: 我也尝试过使用 cimport,但遗憾的是问题没有改变。
【问题讨论】:
-
如果没有实际的minimal reproducible example,很难确定,但我想您必须使用
cimport AstBase而不是import。这让 Cython 在编译时计算出类型 -
@DavidW 不幸的是,这不起作用,此外我添加了原始导入语句。谢谢你的回答!关于最小可重现示例:我仅使用我提供的代码对其进行了测试。不幸的是,错误仍然存在。您可以尝试复制/粘贴我所做的并编辑
AstPreprocessor的导入。你应该得到完全相同的错误。 -
如果没有 pxd-file,cimport 将不起作用。
-
@ead 谢谢你的回答。
标签: python cython subclassing cythonize