Metaclass Mixin or Chaining?

A type can have only one metaclass, because a metaclass simply states what the class statement does – having more than one would make no sense. For the same reason “chaining” makes no sense: the first metaclass creates the type, so what is the 2nd supposed to do?

You will have to merge the two metaclasses (just like with any other class). But that can be tricky, especially if you don’t really know what they do.

class MyModelBase(type):
    def __new__(cls, name, bases, attr):
        attr['MyModelBase'] = 'was here'
        return type.__new__(cls,name, bases, attr)

class MyMixin(type):
    def __new__(cls, name, bases, attr):
        attr['MyMixin'] = 'was here'
        return type.__new__(cls, name, bases, attr)

class ChainedMeta(MyModelBase, MyMixin):
    def __init__(cls, name, bases, attr):
        # call both parents
        MyModelBase.__init__(cls,name, bases, attr)
        MyMixin.__init__(cls,name, bases, attr)

    def __new__(cls, name, bases, attr):
        # so, how is the new type supposed to look?
        # maybe create the first
        t1 = MyModelBase.__new__(cls, name, bases, attr)
        # and pass it's data on to the next?
        name = t1.__name__
        bases = tuple(t1.mro())
        attr = t1.__dict__.copy()
        t2 = MyMixin.__new__(cls, name, bases, attr)
        return t2

class Model(object):
    __metaclass__ = MyModelBase # inherits from `ModelBase`

class MyModel(Model):
    __metaclass__ = ChainedMeta

print MyModel.MyModelBase
print MyModel.MyMixin

As you can see this is involves some guesswork already, since you don’t really know what the other metaclasses do. If both metaclasses are really simple this might work, but I wouldn’t have too much confidence in a solution like this.

Writing a metaclass for metaclasses that merges multiple bases is left as an exercise to the reader ;-P

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)