我将Python类转换为.pyx文件中的扩展类型.我可以在另一个Cython模块中创建这个对象,但我不能用它做静态类型.
这是我班级的一部分:
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...
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用.pxd文件来声明它,但它在所有函数上都说"C方法[某些函数]已声明但未定义".我也尝试在我的实现的功能中剥离C的东西,使它像一个增强类,但这也不起作用.
这是我的.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)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
Spe*_*ZAR 12
我找到了解决方法.这是解决方案:
在.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
Run Code Online (Sandbox Code Playgroud)
在.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)
Run Code Online (Sandbox Code Playgroud)
在任何Cython模块(.pyx)我想用它:
cimport pattern_tree
from pattern_tree cimport PatternTree
Run Code Online (Sandbox Code Playgroud)
最后一句警告:Cython 不支持相对导入.这意味着您必须提供相对于您正在执行的主文件的整个模块路径.
希望这有助于那里的人.
| 归档时间: |
|
| 查看次数: |
1696 次 |
| 最近记录: |