Never mind, I've figured it out. The build_class function looks at the '__class__' attribute of the first base class if there's no explicit __metaclass__ attribute. By calling type directly, the __class__ attribute as returned by MyMeta is, in fact, type. <br>
<br>Should have just looked at the source to begin with.<br><br><div><span class="gmail_quote">On 2/21/08, <b class="gmail_sendername">Jeff McNeil</b> <<a href="mailto:jeff@jmcneil.net">jeff@jmcneil.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
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><span class="sg"><br>Jeff<br><br>
</span></blockquote></div><br>