Why does the class definition's metaclass keyword argument accept a callable?
<h2>Background</h2>

<p>The Python 3 <a href="https://docs.python.org/3.6/reference/datamodel.html#determining-the-appropriate-metaclass">documentation</a> clearly describes how the metaclass of a class is determined:</p>

<blockquote>
  <ul>
  <li>if no bases and no explicit metaclass are given, then type() is used</li>
  <li>if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass</li>
  <li>if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used</li>
  </ul>
</blockquote>

<p>Therefore, according to the second rule, it is possible to specify a metaclass using a callable.  E.g.,</p>

<pre><code>class MyMetaclass(type):
    pass

def metaclass_callable(name, bases, namespace):
    print("Called with", name)
    return MyMetaclass(name, bases, namespace)

class MyClass(metaclass=metaclass_callable):
    pass

class MyDerived(MyClass):
    pass

print(type(MyClass), type(MyDerived))
</code></pre>

<h2>Question 1</h2>

<p>Is the metaclass of <code>MyClass</code>: <code>metaclass_callable</code> or <code>MyMetaclass</code>?  The second rule in the documentation says that the provided callable "is used directly as the metaclass".  However, it seems to make more sense to say that the metaclass is <code>MyMetaclass</code> since</p>

<ul>
<li><code>MyClass</code> and <code>MyDerived</code> have type <code>MyMetaclass</code>,</li>
<li><code>metaclass_callable</code> is called once and then appears to be unrecoverable,</li>
<li>derived classes do not use (as far as I can tell) <code>metaclass_callable</code> in any way (they use <code>MyMetaclass</code>).</li>
</ul>

<h2>Question 2</h2>

<p>Is there anything you can do with a callable that you can't do with an instance of <code>type</code>?  What is the purpose of accepting an arbitrary callable?</p>

<p>Well, the <code>type</code> is of course <code>MyMetaClass</code>. <code>metaclass_callable</code> is initially 'selected' as the metaclass since <a href="https://github.com/python/cpython/blob/master/Python/bltinmodule.c#L100" rel="nofollow">it's been specified in the <code>metaclass</code> kwarg</a> and as such, it's <code>__call__</code> (a simple function call) is going to be performed. </p>

<p>It just so happens that calling it will <code>print</code> and then invoke <code>MyMetaClass.__call__</code> (which calls <code>type.__call__</code> since <code>__call__</code> hasn't been overridden for <code>MyMetaClass</code>). <a href="https://github.com/python/cpython/blob/master/Objects/typeobject.c#L2693" rel="nofollow"><em>There</em> the assignment of <code>cls.__class__</code> is made</a> to <code>MyMetaClass</code>.  </p>

<blockquote>
  <p><code>metaclass_callable</code> is called once and then appears to be unrecoverable</p>
</blockquote>

<p>Yes, it is only initially invoked and then hands control over to <code>MyMetaClass</code>. I'm not aware of any class attribute that keeps that information around. </p>

<blockquote>
  <p>derived classes do not use (as far as I can tell) <code>metaclass_callable</code> in any way.</p>
</blockquote>

<p>Nope, if no <code>metaclass</code> is explicitly defined, <a href="https://github.com/python/cpython/blob/master/Python/bltinmodule.c#L130" rel="nofollow">the best match for the metaclasses of <code>bases</code></a> (here <code>MyClass</code>) will be used (resulting in <code>MyMetaClass</code>).</p>

<hr>

<p>As for question <code>2</code>, pretty sure everything you can do with a callable is also possible by using an instance of type with <code>__call__</code> overridden accordingly. As to <em>why</em>, you might not want to go full blown class-creation if you simply want to make minor changes when actually creating a class.</p>

python class python-3.x metaclass