metaclass multiple inheritance inconsistency

Posted by Matt Anderson on Stack Overflow See other posts from Stack Overflow or by Matt Anderson
Published on 2010-06-09T04:15:33Z Indexed on 2010/06/09 4:22 UTC
Read the original article Hit count: 378

Filed under:
|

Why is this:

class MyType(type):
    def __init__(cls, name, bases, attrs):
        print 'created', cls
class MyMixin:
    __metaclass__ = MyType
class MyList(list, MyMixin): pass

okay, and works as expected:

created <class '__main__.MyMixin'>
created <class '__main__.MyList'>

But this:

class MyType(type):
    def __init__(cls, name, bases, attrs):
        print 'created', cls
class MyMixin:
    __metaclass__ = MyType
class MyObject(object, MyMixin): pass

Is not okay, and blows up thusly?:

created <class '__main__.MyMixin'>
Traceback (most recent call last):
  File "/tmp/junk.py", line 11, in <module>
    class MyObject(object, MyMixin): pass
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases object, MyMixin

© Stack Overflow or respective owner

Related posts about python

Related posts about metaclass