cdef类的python列表,避免所有显式强制转换

cdef类的python列表,避免所有显式强制转换,python,types,cython,Python,Types,Cython,我正在围绕外部C库编写Cython包装。该库生成一个树数据结构(它是一个HTML解析器),Cython代码需要构造二级树并遍历它们。我有这样的类定义: cdef class TreeNode: cdef list children # List<TreeNode> cpdef debug_dump(TreeNode self, object fp) # ... other fields and methods ... cpdef debug_dump(

我正在围绕外部C库编写Cython包装。该库生成一个树数据结构(它是一个HTML解析器),Cython代码需要构造二级树并遍历它们。我有这样的类定义:

cdef class TreeNode:
    cdef list children  # List<TreeNode>

    cpdef debug_dump(TreeNode self, object fp)

    # ... other fields and methods ...
cpdef debug_dump(TreeNode self, object fp):
    fp.write("... stuff about self ...")

    for c in self.children:
        (<TreeNode>c).debug_dump(fp)
cdef类树节点:
cdef列表子项#列表
cpdef调试_转储(树节点自身,对象fp)
# ... 其他领域和方法。。。
然后是这样做的方法:

cdef class TreeNode:
    cdef list children  # List<TreeNode>

    cpdef debug_dump(TreeNode self, object fp)

    # ... other fields and methods ...
cpdef debug_dump(TreeNode self, object fp):
    fp.write("... stuff about self ...")

    for c in self.children:
        (<TreeNode>c).debug_dump(fp)
cpdef调试_转储(TreeNode self,object fp):
fp.写(“…关于自我的东西…”)
对于self.children中的c:
(c) .debug_dump(fp)
这一切都是可行的,但我发现自己不得不到处写
(x)
。在某些情况下,它变得非常可笑:

# Otherwise, this block might be mergeable with its immediate
# previous sibling.
else:
    pclass = (<TreeNode>(<TreeNode>self.stack[-1]).children[-1]).tagclass
    if (tclass == pclass or (tclass == TC_PARA and pclass == TC_HEADING)):
        self.stack.append((<TreeNode>self.stack[-1]).children.pop())
        (<TreeNode>self.stack[-1]).tagclass = TC_PARA
        (<TreeNode>self.stack[-1])._depth = <int>depth
#否则,此块可能会与其直接
#以前的兄弟姐妹。
其他:
pclass=((self.stack[-1]).children[-1]).tagclass
如果(tclass==pclass或(tclass==TC_PARA和pclass==TC_HEADING)):
self.stack.append((self.stack[-1]).children.pop())
(self.stack[-1]).tagclass=TC_PARA
(自堆栈[-1])。_depth=深度
有没有办法声明一个变量是某个具体类型的列表,这样我就不必到处散布这些类型转换了

(注意:我知道在<代码> -CPLUS 模式中使用<代码>向量<代码>的可能性,但是这是一个与外部库的接口,我不确定使用C++的头文件是安全的,所以我更喜欢一个不包含<代码> -CPLUS < /C>模式的解决方案。未来的hon代码,AFAIK将无法使用
vector