TypeError: Cannot create a consistent method resolution order (MRO) for bases object
digitalorganics at gmail.com
digitalorganics at gmail.com
Mon Jun 26 15:49:01 EDT 2006
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()
Simon Forman wrote:
> digitalorganics at gmail.com wrote:
> > 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
More information about the Python-list
mailing list