Initialize class but with different parents in python
<p>In python, is there a way, when initializing a Class, to change the superclass in function of the value of a class attribute? Here's an example of what I want to do. First I have theses classes:</p>

<pre><code>class A(object):
    pass

class B(A):
    # extend and override class A
    pass

class C(A or B):
    # extend and override class A
    pass
</code></pre>

<p>Secondly, I want to create other classes that inherit from <code>Class C</code> but in some cases I want C to inherit from A and on other cases, inherit from B:</p>

<pre><code>class D(C):
    # C inherit only from A
    from_B = False

class E(C):
    # C inherit from B because attribute from_B = True
    from_B = True
</code></pre>

<p>I tried with metaclass but that was setting the base class of <code>C</code> (to <code>A</code> or <code>B</code>) for all subclasses (<code>D</code>, <code>E</code>, ...) at the initialization of the first subclass. So, if the first subclass to be initialize had <code>from_B = True</code>, all subclasses of <code>C</code> had <code>C(B)</code> as parent whatever <code>from_B</code> was set. My code was something like this:</p>

<pre><code>class MetaC(type):
    def __new__(cls, name, bases, attrs):
        if C in bases and getattr(attrs, 'from_B', False):
            C.__bases__[C.__bases__.index(A)] = B
        return super(MetaC, cls).__new__(cls, name, bases, attrs)

class C(A):
    __metaclass__ = MetaC
</code></pre>

<p>My goal is to avoid the duplication of the code of the <code>C</code> class and keeping the possibility to have or not the added functionalities of the <code>B</code> class. I should mention that I don't have control on <code>A</code> and <code>B</code> classes.</p>

<h3>UPDATE</h3>

<p>I think I got it with this metaclass (code is a bit rough at the moment):</p>

<pre><code>class MetaC(type):
    def __new__(cls, name, bases, attrs):
        for base in bases:
            if base.__name__ == 'C':
                if attrs.has_key('from_B'):
                    list_bases = list(base.__bases__)
                    list_bases[list_bases.index(A)] = B
                    base.__bases__ = tuple(list_bases)
                elif B in base.__bases__:
                    list_bases = list(base.__bases__)
                    list_bases[list_bases.index(B)] = A
                    base.__bases__ = tuple(list_bases)
                break
        return super(MetaC, cls).__new__(cls, name, bases, attrs)
</code></pre>

<h3>UPDATE 2</h3>

<p>This solution doesn't work because I'm always modifying the base class C. So, when a subclass is instanciated it will use  the C class in it's current state. </p>
