如何将 collections.abc 类 用于多重继承?

How is one meant to use the collections.abc classes with multiple inheritance?

我遇到了一个问题,我想使用来自 collections.abc.MutableSequence 的混合方法,但我还必须继承其他东西。

class Thing(urwid.Pile, collections.abc.MutableSequence):
    ...

我最终得到

TypeError: metaclass conflict: the metaclass of a derived class must be
a (non-strict) subclass of the metaclasses of all its bases

我如何确定发生了什么并修复它? metaclass = ABCMeta 没用,不管它值多少钱。

metaclass=ABCMeta 的问题。 MutableSequence 使用 ABCMeta 作为其元类,Pile 使用其他东西,因此发生冲突。

你可以做的是从 Pile 继承并使用 MutableSequence.register(),像这样:

class Thing(urwid.Pile):
    ...

collections.abc.MutableSequence.register(Thing)

如果您的 Thing 没有实现所有必需的方法,您不会得到异常,但是 issubclass(Thing, MutableSequence)isinstance(Thing(), MutableSequence) 将 return 为真。