在Cython中共享扩展类型以进行静态输入 [英] Share extension types in Cython for static typing
问题描述
我将Python类转换为.pyx文件中的扩展类型。我可以在其他Cython模块中创建该对象,但是我不能对其进行静态输入。
I converted a Python class to an extension type inside a .pyx file. I can create this object in the other Cython module, but I can't do static typing with it.
这是我的一部分class:
Here is a part of my class:
cdef class PatternTree:
cdef public PatternTree previous
cdef public PatternTree next
cdef public PatternTree parent
cdef public list children
cdef public int type
cdef public unicode name
cdef public dict attributes
cdef public list categories
cdef public unicode output
def __init__(self, name, parent=None, nodeType=XML):
# Some code
cpdef int isExpressions(self):
# Some code
cpdef MatchResult isMatch(self, PatternTree other):
# Some code
# More functions...
我尝试使用.pxd文件声明它,但是它说 C方法[一些功能]声明但未定义指示。我还尝试在实现的功能中剥离C内容,使其表现为增强类,但这也不起作用。
I have tried using a .pxd file to declare it, but it says "C method [some function] is declared but not defined" on all of my functions. I have also tried stripping the C stuff in the functions of my implementation to make it act like an augmented class, but that didn't work either.
这是我的。 pxd表示:
Here is my .pxd as it stands:
cdef class PatternTree:
cdef public PatternTree previous
cdef public PatternTree next
cdef public PatternTree parent
cdef public list children
cdef public int type
cdef public unicode name
cdef public dict attributes
cdef public list categories
cdef public unicode output
# Functions
cpdef int isExpressions(self)
cpdef MatchResult isMatch(self, PatternTree other)
谢谢您的帮助!
推荐答案
我找到了解决办法。解决方法如下:
I found out a fix for it. Here is the solution:
在.pyx中:
cdef class PatternTree:
# NO ATTRIBUTE DECLARATIONS!
def __init__(self, name, parent=None, nodeType=XML):
# Some code
cpdef int isExpressions(self):
# Some code
cpdef MatchResult isMatch(self, PatternTree other):
# More code
在.pxd中:
cdef class PatternTree:
cdef public PatternTree previous
cdef public PatternTree next
cdef public PatternTree parent
cdef public list children
cdef public int type
cdef public unicode name
cdef public dict attributes
cdef public list categories
cdef public unicode output
# Functions
cpdef int isExpressions(self)
cpdef MatchResult isMatch(self, PatternTree other)
在任何Cython模块(.pyx)中,我都想将其用于:
In whatever Cython module (.pyx) I want to use this for:
cimport pattern_tree
from pattern_tree cimport PatternTree
最后一个警告词:Cython 不支持相对导入。这意味着您必须提供相对于要执行的主文件的整个模块路径。
One final word of warning: Cython does not support relative importing. This means that you have to supply the entire module path relative to the main file you're executing from.
希望这对那里的人有所帮助。
Hopes this helps somebody out there.
这篇关于在Cython中共享扩展类型以进行静态输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!