Inheritance of metaclass
<p>In this well known <a href="https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">answer</a> that explains metaclass in Python. It mentions that the <code>__metaclass__</code> attribute will not be inherited.</p>

<p>But as a matter of fact, I tried in Python:</p>

<pre><code>class Meta1(type):
    def __new__(cls, clsname, bases, dct):
        print "Using Meta1"
        return type.__new__(cls, clsname, bases, dct)

# "Using Meta1" printed
class Foo1:
    __metaclass__ = Meta1

# "Using Meta1" printed
class Bar1(Foo1):
    pass
</code></pre>

<p>As expected, both <code>Foo</code> and <code>Bar</code> use <code>Meta1</code> as metaclass and print string as expected. </p>

<p>But in the following sample, when <code>type(...)</code> is returned instead of <code>type.__new__(...)</code>, the metaclass is no longer inherited:</p>

<pre><code>class Meta2(type):
    def __new__(cls, clsname, bases, dct):
        print "Using Meta2"
        return type(clsname, bases, dct)

# "Using Meta2" printed
class Foo2:
    __metaclass__ = Meta2

# Nothing printed
class Bar2(Foo2):
    pass
</code></pre>

<p>Inspecting the <code>__metaclass__</code> and <code>__class__</code> attributes, I can see:</p>

<pre><code>print Foo1.__metaclass__ # &lt;class '__main__.Meta1'&gt;
print Bar1.__metaclass__ # &lt;class '__main__.Meta1'&gt;
print Foo2.__metaclass__ # &lt;class '__main__.Meta2'&gt;
print Bar2.__metaclass__ # &lt;class '__main__.Meta2'&gt;

print Foo1.__class__ # &lt;class '__main__.Meta1'&gt;
print Bar1.__class__ # &lt;class '__main__.Meta1'&gt;
print Foo2.__class__ # &lt;type 'type'&gt;
print Bar2.__class__ # &lt;type 'type'&gt;
</code></pre>

<p>In conclusion:</p>

<ol>
<li><p>Both <code>__metaclass__</code> and <code>__class__</code> <strong>will</strong> be inherited from base class.</p></li>
<li><p>The creation behavior defined by <code>Meta2</code> will be used for <code>Foo2</code>, although <code>Foo2.__class__</code> is actually <code>type</code>.</p></li>
<li><p>The <code>__metaclass__</code> attribute in <code>Bar2</code> is <code>Meta2</code>, but the creation behavior of <code>Bar2</code> is not affected. In another word, <code>Bar2</code> uses <code>type</code> as its "real" metaclass instead of <code>Meta2</code>.</p></li>
</ol>

<p>These observations make the inheritance mechanism of <code>__metaclass__</code> kind of vague to me.</p>

<p>My guess is that:</p>

<ol>
<li><p>When directly assigning a class (e.g. <code>Meta1</code>) to the <code>__metaclass__</code> attribute of another class 'Foo1', It's the <code>__metaclass__</code> attribute taking effect.</p></li>
<li><p>When subclass does not explicitly set <code>__metaclass__</code> attribute when defining. The <code>__class__</code> attribute instead of <code>__metaclass__</code> attribute of base class will decide the "real" metaclass of subclass.</p></li>
</ol>

<p>Is my guess correct? How does Python deal with the inheritance of metaclass?</p>