TypeError: Cannot create a consistent method resolution order (MRO) for bases object

Steve Holden steve at holdenweb.com
Tue Jun 27 02:49:57 EDT 2006


digitalorganics at gmail.com wrote:
> Maric Michaud wrote:
> 
>>Le lundi 26 juin 2006 20:06, digitalorganics at gmail.com a écrit :
>>
>>>What are the reason one would get this error: TypeError: Cannot create
>>>a consistent method resolution order (MRO) for bases object ??
>>>
>>>I can provide the code if needed....
>>
>>This is the python solution to the diamond problem (cf. wikipedia).
>>
>>In [61]: class a(object) : pass
>>   ....:
>>In [62]: class b(a) : pass
>>   ....:
>>In [63]: class c(object, a) : pass
>>   ....:
>>---------------------------------------------------------------------------
>>exceptions.TypeError                                 Traceback (most recent
>>call last)
>>
>>/home/maric/<ipython console>
>>
>>TypeError: Error when calling the metaclass bases
>>    Cannot create a consistent method resolution
>>order (MRO) for bases object, a
>>
>>In [64]: b.mro()
>>Out[64]: [<class '__main__.b'>, <class '__main__.a'>, <type 'object'>]
>>
>>In [65]: class c(a, object) : pass
>>   ....:
>>
>>In [66]: c.mro()
>>Out[66]: [<class '__main__.c'>, <class '__main__.a'>, <type 'object'>]
>>
>>In [67]: class d(b, c) : pass
>>   ....:
>>
>>In [69]: d.mro()
>>Out[69]:
>>[<class '__main__.d'>,
>> <class '__main__.b'>,
>> <class '__main__.c'>,
>> <class '__main__.a'>,
>> <type 'object'>]
>>
>>mro means "method resolution order", this is the path the interpreter will
>>look for attributes for a given class. You cannot introduce inconsistency in
>>this path, for example duplicate the type object before another type (or any
>>type wich appear to be the ancestor of another).
>>
>>--
> 
> 
> Ah, thank you Maric, I see the problem. I diagrammed the flow of class
> inheritance and I can see the problem clearly. Ruby doesn't have this
> problem because it uses a single inheritance model complemented by the
> power of mixins (modules of functionality mixed into classes as
> needed). So what's the Pythonic solution to this problem?
> 
Technically, wait until Python 3.0. Until then, just make sure all your 
classes inherit from (something that inherits from ...) object.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list