title: Who calls the metaclass
body: <p>This actually stems from a discussion here on SO.</p>
  
  <p><strong>Short version</strong></p>
  
  <pre><code>def meta(name, bases, class_dict)
      return type(name, bases, class_dict)
  
  class Klass(object):
      __metaclass__ = meta
  </code></pre>
  
  <p><code>meta()</code> is called when <code>Klass</code> class declaration is executed.</p>
  
  <p>Which part of the (python internal) code actually calls <code>meta()</code>?</p>
  
  <p><strong>Long version</strong></p>
  
  <p>When the class is declared, some code has to do the appropriate attribute checks and see if there is a <code>__metaclass__</code> declared on a type. If such exists, it has to perform a method call on that metaclass with the well known <code>(class_name, bases, class_dict)</code> attributes. It is not really clear to me which code is responsible for that call.</p>
  
  <p>I have done some digging in CPython (see below), but I would really like to have something closer to a definite answer.</p>
  
  <p><strong>Option 1: Called directly</strong></p>
  
  <p>The metaclass call is hardwired into the class parsing. If so, is there any evidence for this?</p>
  
  <p><strong>Option 2: It is called by <code>type.__new__()</code></strong></p>
  
  <p>Code in <a href="https://github.com/python/cpython/blob/master/Objects/typeobject.c#L890" rel="nofollow noreferrer"><code>type_call()</code></a> calls <a href="https://github.com/python/cpython/blob/master/Objects/typeobject.c#L2276" rel="nofollow noreferrer"><code>type_new()</code></a> which in turn calls <a href="https://github.com/python/cpython/blob/master/Objects/typeobject.c#L2241" rel="nofollow noreferrer"><code>_PyType_CalculateMetaclass()</code></a>. This suggests that metaclass resolution is actually done during the call to <code>type()</code> when trying to find out which value to return from <code>type()</code></p>
  
  <p>This would be in in line with the notion that a "class" is a "<em>callable that returns an object</em>".</p>
  
  <p><strong>Option 3: Something different</strong></p>
  
  <p>All my guesses maybe completely wrong, of course.</p>
  
  <blockquote>
    <p>Some example cases that we came up with in chat:</p>
  </blockquote>
  
  <p><strong>Example 1:</strong></p>
  
  <pre><code>class Meta(type):
      pass
  
  class A:
      __metaclass__ = Meta
  
  A.__class__ == Meta
  </code></pre>
  
  <p>This is what <code>Meta.__new__()</code> returns, so this seems legit. The metaclass puts itself as <code>A.__class__</code></p>
  
  <p><strong>Example 2:</strong></p>
  
  <pre><code>class Meta(type):
      def __new__(cls, class_name, bases, class_dict):
          return type(class_name, bases, class_dict)
  
  class A(object):
      __metaclass__ = Meta
  
  A.__class__ == type
  </code></pre>
  
  <p>Edit 2: correct initial version, properly derive <code>Meta</code> from <code>type</code>.</p>
  
  <p>Seems okay enough, but I'm not really sure this does what I think it does. Also: What is the canonical method to make it behave like in Example 1?</p>
  
  <p>Edit 3: Using <code>type.__new__(...)</code> seems to work as expected, which also seems in favor of Option 2.</p>
  
  <p>Can anybody with more in-depth knowledge of internal python magic enlighten me?</p>
  
  <p>Edit: A for a quite concise primer on metaclasses: <a href="http://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/" rel="nofollow noreferrer">http://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/</a>. Also has some really nice diagrams, references and also highlights the differences between python 2 and 3.</p>
  
  <p>Edit 3: There is a good answer below for Python 3. Python 3 uses <a href="https://stackoverflow.com/questions/1832997/what-does-pythons-builtin-build-class-do"><code>__build_class__</code></a> to create a class object. The code path is --however-- different in Python 2.</p>