Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将collections.abc类与多重继承一起使用?_Python_Python 3.x_Multiple Inheritance_Metaclass - Fatal编程技术网

Python 如何将collections.abc类与多重继承一起使用?

Python 如何将collections.abc类与多重继承一起使用?,python,python-3.x,multiple-inheritance,metaclass,Python,Python 3.x,Multiple Inheritance,Metaclass,我遇到了一个问题,我想使用collections.abc.MutableSequence中的mixin方法,但我还必须继承其他方法 类事物(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 i

我遇到了一个问题,我想使用
collections.abc.MutableSequence
中的mixin方法,但我还必须继承其他方法

类事物(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)
将返回True。

元类=ABCMeta
是问题所在
MutableSequence
使用
ABCMeta
作为它的元类,
Pile
使用其他东西,因此产生了冲突

您可以从
Pile
继承并使用
MutableSequence.register()
,如下所示:

class Thing(urwid.Pile):
    ...

collections.abc.MutableSequence.register(Thing)

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

谢谢。类ABCMetaWidgetMeta(urwid.WidgetMeta,ABCMeta):传递并设置它,因为元类似乎也解决了它。@dwf:注意
WidgetMeta
必须与
ABCMetaWidgetMeta
兼容才能正确工作。有时候,继承在没有任何代码行的情况下不会“正常工作”:在这种情况下,它们是完全正交的。WidgetMeta只是做了一些属性缓存。谢谢。类ABCMetaWidgetMeta(urwid.WidgetMeta,ABCMeta):传递并设置它,因为元类似乎也解决了它。@dwf:注意
WidgetMeta
必须与
ABCMetaWidgetMeta
兼容才能正确工作。有时候,继承在没有任何代码行的情况下不会“正常工作”:在这种情况下,它们是完全正交的。WidgetMeta只是做一些属性缓存。