Metaclass multiple inheritance inconsistency

≯℡__Kan透↙ 提交于 2019-11-30 02:37:16

It's not a custom-metaclass problem (though it's diagnosed at metaclass stage):

>>> class Normal(object): pass
... 
>>> class MyObject(object, Normal): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases object, Normal

and the problem's just the same as this one:

>>> class Derived(Normal): pass
... 
>>> class Ok(Derived, Normal): pass
... 
>>> class Nope(Normal, Derived): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases Normal, Derived

i.e., can't multiply inherit from a base class followed by a derived class -- it's impossible to define a consistent MRO that satisfies the usual MRO constraints/guarantees.

Fortunately, you don't want to do that -- the subclass presumably overrides some method of the base class (that's what normal subclasses do;-), and having the base class "in front" would mean "shadowing the override away".

Putting the base class after the derived one is pretty useless, but at least it's innocuous (and consistent with normal MRO guarantees).

Your first example of course works because MyMixin is not derived from list:

>>> MyMixin.__mro__
(<class '__main__.MyMixin'>, <type 'object'>)

...but it is derived from object (like every modern-style Python class), so the second example cannot work (quite independently from MyMixin having a custom metaclass).

Talat Parwez

Here, you are inheriting the parent class, and the parent class is already inheriting another class, so there is no need to inherit the class that the parent class already inherited.

For example:

class A(object):
.
.
class B(object, A):
.
.

It will throw an error because A is inheriting the class Object and B is inheriting the A, so indirectly B is inheriting object, so there is no need to inherit object. . . .

The Solution is to just remove the object class from class B ... arguments list.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!