【发布时间】:2018-10-06 22:51:03
【问题描述】:
我有一些类在 cython 中实现为 cdef class。在客户端 python 代码中,我想组合具有多重继承的类,但我遇到了类型错误。这是一个最小的可重现示例:
In [1]: %load_ext cython
In [2]: %%cython
...: cdef class A:
...: cdef int x
...: def __init__(self):
...: self.x = 0
...: cdef class B:
...: cdef int y
...: def __init__(self):
...: self.y = 0
...:
In [3]: class C(A, B):
...: pass
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-83ef5091d3a6> in <module>()
----> 1 class C(A, B):
2 pass
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict
有没有办法解决这个问题?
docs 这么说:
一个 Python 类可以从多个扩展类型继承,前提是遵循多继承的常用 Python 规则(即所有基类的 C 布局必须兼容)。
鉴于上面的简单示例,我试图理解这可能意味着什么。
【问题讨论】:
标签: python cython multiple-inheritance