Multiple inheritance : waht does this error mean ?

Peter Otten __peter__ at web.de
Tue May 16 08:40:10 CEST 2006


jnair at ensim.com wrote:

> I am using Python 2.4.3
> 
>>>>class K(object,list):
>    ...:     pass
>    ...:
> ------------------------------------------------------------
> Traceback (most recent call last):
>   File "<console>", line 1, in ?
> TypeError: Error when calling the metaclass bases
>     Cannot create a consistent method resolution
> order (MRO) for bases object, list

class K(object, list): pass

specifies that a method should be looked up first in object, then in list.
As list inherits from object, its method resolution order 

>>> list.__mro__
(<type 'list'>, <type 'object'>)

specifies that a method should be looked up first in list, then in object.
Python refuses to resolve the conflict for you, but you can do it manually:

>>> class K2(list, object): pass
...
>>> K2.__mro__
(<class '__main__.K2'>, <type 'list'>, <type 'object'>)

Now K2, list, and object can agree to look for a method in K2, then list,
then object. However, you get the same effect by just inheriting from list:

>>> class K3(list): pass
...
>>> K3.__mro__
(<class '__main__.K3'>, <type 'list'>, <type 'object'>)

Peter



More information about the Python-list mailing list