Is it possible to dynamically create a metaclass for a class with several bases, in Python 3?
<p>In Python 2, with a trick it is possible to create a class with several bases, although the bases have metaclasses that are <em>not</em> subclass of each other.</p>

<p>The trick is that these metaclasses have themselves a metaclass (name it a "metametaclass"), and this metametaclass provides the metaclasses with a call method that dynamically creates a common sub-metaclass of the base metaclasses, if necessary. Eventually, a class results whose metaclass is the new sub-metaclass. Here is the code:</p>

<pre><code>&gt;&gt;&gt; class MetaMeta(type):
...     def __call__(mcls, name, bases, methods):
...         metabases = set(type(X) for X in bases)
...         metabases.add(mcls)
...         if len(metabases) &gt; 1:
...             mcls = type(''.join([X.__name__ for X in metabases]), tuple(metabases), {})
...         return mcls.__new__(mcls, name, bases, methods)
... 
&gt;&gt;&gt; class Meta1(type):
...     __metaclass__ = MetaMeta
... 
&gt;&gt;&gt; class Meta2(type):
...     __metaclass__ = MetaMeta
... 
&gt;&gt;&gt; class C1:
...     __metaclass__ = Meta1
... 
&gt;&gt;&gt; class C2:
...     __metaclass__ = Meta2
... 
&gt;&gt;&gt; type(C1)
&lt;class '__main__.Meta1'&gt;
&gt;&gt;&gt; type(C2)
&lt;class '__main__.Meta2'&gt;
&gt;&gt;&gt; class C3(C1,C2): pass
... 
&gt;&gt;&gt; type(C3)
&lt;class '__main__.Meta1Meta2'&gt;
</code></pre>

<p>This example (of course changing the syntax to <code>class C1(metaclass=Meta1)</code> etc) doesn't work in Python 3.</p>

<p><strong>Question 1:</strong> Do I understand correctly that in Python 2, first <code>C3</code> is constructed, using the metaclass of the first base, and an error would only result if <code>type(C3)</code> were not a common subclass of <code>type(C1)</code> and <code>type(C2)</code>, whereas in Python 3 the error is raised earlier?</p>

<p><strong>Question 2:</strong> (How) Is it possible to make the above example work in Python 3? I did try to use a subclass of <code>abc.ABCMeta</code> as metametaclass, but even though using a custom <code>__subclasscheck__</code> makes <code>issubclass(Meta1, Meta2)</code> return <code>True</code>, the creation of C3 would still result in an error.</p>

<p><strong>Note:</strong> Of course I could make Python 3 happy by statically defining <code>Meta1Meta2</code> and explicitly using it as a metaclass for <code>C3</code>. However, that's not what I want. I want that the common sub-metaclass is created dynamically.</p>

<p>In 95% of cases, it should be possible to use the machinery introduced in Python 3.6 due to <a href="https://www.python.org/dev/peps/pep-0447" rel="nofollow">PEP 447</a> to do <em>most</em> of what metaclasses can do using special new hooks.  In that case, you will not need to combine metaclasses since your hooks can call super and their behavior is combined due to inheritance.</p>

<p>As for your general case, I believe that mgilson is right and that you are probably making things too complicated.  I have yet to see a case for combining metaclasses that is not covered by PEP 447.</p>

python metaclass