Python- Multiple dynamic inheritance
<p>I'm having trouble with getting multiple dynamic inheritance to work.  These examples make the most sense to me(<a href="http://stackoverflow.com/questions/21060073/dynamic-inheritance-in-python">here</a> and <a href="http://stackoverflow.com/questions/32104219/dynamic-multiple-inheritance-in-python">here</a>), but there's not enough code in one example for me to really understand what's going on and the other example doesn't seem to be working when I change it around for my needs (code below).</p>

<p>I'm creating a universal tool that works with multiple software packages. In one software, I need to inherit from 2 classes: 1 software specific API mixin, and 1 PySide class.  In another software I only need to inherit from the 1 PySide class.  </p>

<p>The least elegant solution that I can think of is to just create 2 separate classes (with all of the same methods) and call either one based on the software that's running.  I have a feeling there's a better solution.</p>

<p>Here's what I'm working with:</p>

<pre><code>## MainWindow.py

import os
from maya.app.general.mayaMixin import MayaQWidgetDockableMixin

# Build class
def build_main_window(*arg):

    class Build(arg):
        def __init__(self):
            super( Build, self ).__init__()

        # ----- a bunch of methods

# Get software
software = os.getenv('SOFTWARE')

# Run tool
if software == 'maya':
    build_main_window(maya_mixin_class, QtGui.QWidget)
if software == 'houdini':
    build_main_window(QtGui.QWidget)
</code></pre>

<p>I'm currently getting this error:</p>

<pre><code>#     class Build(arg):
# TypeError: Error when calling the metaclass bases
#     tuple() takes at most 1 argument (3 given) # 
</code></pre>

<p>Thanks for any help!</p>

<h1>EDIT:</h1>

<pre><code>## MainWindow.py

import os

# Build class 
class BuildMixin():
    def __init__(self):
        super( BuildMixin, self ).__init__()

    # ----- a bunch of methods

def build_main_window(*args):
    return type('Build', (BuildMixin, QtGui.QWidget) + args, {})

# Get software
software = os.getenv('SOFTWARE')

# Run tool
if software == 'maya':
    from maya.app.general.mayaMixin import MayaQWidgetDockableMixin

    Build = build_main_window(MayaQWidgetDockableMixin)

if software == 'houdini':
    Build = build_main_window()
</code></pre>

<p><code>arg</code> is a tuple, you can't use a tuple as a base class.</p>

<p>Use <code>type()</code> to create a new class instead; it takes a class name, a tuple of base classes (can be empty) and the class body (a dictionary).</p>

<p>I'd keep the methods for your class in a mix-in method:</p>

<pre><code>class BuildMixin():
    def __init__(self):
        super(BuildMixin, self).__init__()

    # ----- a bunch of methods

def build_main_window(*arg):
    return type('Build', (BuildMixin, QtGui.QWidget) + args, {})

if software == 'maya':
    Build = build_main_window(maya_mixin_class)
if software == 'houdini':
    Build = build_main_window()
</code></pre>

<p>Here, <code>args</code> is used as an <em>additional</em> set of classes to inherit from. The <code>BuildMixin</code> class provides all the real methods, so the third argument to <code>type()</code> is left empty (the generated <code>Build</code> class has an empty class body).</p>

<p>Since <code>QtGui.QWidget</code> is common between the two classes, I just moved that into the <code>type()</code> call.</p>

python class inheritance dynamic