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

D

digitalorganics

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....
 
S

Simon Forman

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....

Yes, do that.

That's an amazing error.

~Simon
 
T

Terry Reedy

What are the reason one would get this error: TypeError: Cannot create
a consistent method resolution order (MRO) for bases object ??

Because the interpreter cannot ;-)
I can provide the code if needed....

Details beget details ;-)
Yes, the class statement (header line up to ':') that bombed and the same
(first line) for classes it inherits from (and their base classes). The
conflict should be in the latter.

tjr
 
D

digitalorganics

Hint: If I change Dog and Bat to old-style classes, there's no problem,
everything works fine.


Okay, here's the code dump from my playground::
------------------------------------------
#!/usr/bin/env python

class Mixin:
def mixin(object, *classes):
NewClass = type('Mixin', (object.__class__,) + classes, {})
newobj = NewClass()
newobj.__dict__.update(object.__dict__)
return newobj

class Cat(object):
def __init__(self):
self.love = 0
def meow(self):
print "meow"
class Dog(object):
def bark(self):
print "bark"
class Bat(object):
def scream(self):
print "scream"

mycat = Cat()
mycat.love = 4
mycat.__class__.__bases__ += (Mixin,)
mycat.mixin(Dog, Bat)
print mycat.love

def isClass(object):
if isinstance(object, type):
return True
elif isinstance(object, types.ClassType):
return True
else:
return False

def listClasses():
classes = []
for eachobj in globals().keys():
if isClass(globals()[eachobj]):
classes.append(globals()[eachobj])
print eachobj
return classes

def applyMixinGlobal(*Mixins):
for eachclass in listClasses():
MixInto(eachclass, Mixins)
#applyMixinGlobal(Mixin)

def MixInto(Class, *Mixins):
for eachMixin in Mixins:
if eachMixin not in Class.__bases__:
Class.__bases__ += (eachMixin,)
MixInto(Bat, Mixin)
Bat.__bases__ += (Dog,)
dargo = Bat()
dargo = dargo.mixin(Cat)
dargo.meow()
 
D

digitalorganics

Oh, I forgot the line that bombed, sorry:

line 52, in __main__
Bat.__bases__ += (Dog,)
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, Mixin, Dog

See my other post for the complete code and my relevant note about
new-style classes, thanks...
 
M

Maric Michaud

Le lundi 26 juin 2006 20:06, (e-mail address removed) 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).

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
D

digitalorganics

Maric said:
Le lundi 26 juin 2006 20:06, (e-mail address removed) 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?
 
S

Steve Holden

Maric said:
Le lundi 26 juin 2006 20:06, (e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top