Hi list,<br><br>Hopefully a quick metaclass question. In the following example, MyMeta is a metaclass that does not inherit directly from type:<br><br>#!/usr/bin/python<br><br>class MyMeta(object):<br> def __new__(cls, name, bases, vars):<br>
print "MyMeta.__new__ called for %s" % name<br> return type(name, bases, vars)<br><br>class MetaWrapper(object):<br> __metaclass__ = MyMeta<br><br>class M(MetaWrapper):<br> pass<br><br>[jeff@marvin ~]$ python t.py <br>
MyMeta.__new__ called for MetaWrapper<br>[jeff@marvin ~]$ <br><br>When I run that script, it's apparent that although M inherits from MetaWrapper, it does not use MyMeta as it's metaclass. However, if I change MyMeta to be a subclass of builtin type, it works as I would expect:<br>
<br>[jeff@marvin ~]$ cat t.py <br>#!/usr/bin/python<br><br>class MyMeta(type):<br> def __new__(cls, name, bases, vars):<br> print "MyMeta.__new__ called for %s" % name<br> return super(MyMeta, cls).__new__(cls, name, bases, vars)<br>
<br>class MetaWrapper(object):<br> __metaclass__ = MyMeta<br><br>class M(MetaWrapper):<br> pass<br><br>[jeff@marvin ~]$ python t.py <br>MyMeta.__new__ called for MetaWrapper<br>MyMeta.__new__ called for M<br>[jeff@marvin ~]$ <br>
<br>How exactly does Python choose which MC it will use when building a class? It doesn't seem to me that the parent class of MyMeta should matter in this case?<br><br>Thanks!<br><br>Jeff<br><br>