Python: shared methods and attributes in multiple inheritance
<p>I simply want to create a class which inherits attributes and methods from two parents. Let's just say the two parent classes are</p>

<pre><code>class A(object):
    def __init__(self, a):
        self.a = a

    def method_a(self):
        return self.a + 10


class B(object):
    def __init__(self, b):
        self.b = b

    def method_b(self):
        return self.b + 20
</code></pre>

<p>Then I am not sure how to create a subclass C which inherits both methods and attributes from its parents. I can do something like this, but I am quite sure this is not very pythonic...</p>

<pre><code>class C(A, B):
    def __init__(self, a, b):
        A.__init__(self, a=a)
        B.__init__(self, b=b)
</code></pre>

<p>then I can do this without a problem</p>

<pre><code>my_class = C(a=1, b=2)
print(my_class.a)
print(my_class.b)
print(my_class.method_a())
print(my_class.method_b())
</code></pre>

<p>I don't know how to set up <code>super</code> to inherit methods and attributes from both parents and I would appreciate any help! And by the way: Classes A and B should not depend on each other.</p>

<p>As far as I know, the way <code>super</code> works is that, based on the list of superclasses declared at the beginning of the subclass, an <code>mro</code> (method resolution order) list will be worked out. When you call <code>supper</code> (Python 3), the <code>__init__</code> of the first class in the <code>mro</code> list will be called. That <code>__init__</code> is also expected to contain another <code>super()</code> so that the <code>__init__</code> of the next class in the <code>mro</code> also gets called.</p>

<p>In your case, the <code>mro</code> list should be <code>[A, B]</code>. So the <code>__init__</code> of <code>A</code> must contain an invocation of <code>super</code> so that the <code>__init__</code> of <code>B</code> is also get called.</p>

<p>The problems here are:</p>

<ul>
<li>You want <code>A</code> and <code>B</code> not to depend of each other. The use of <code>super</code> needs an <code>mro</code> list, which does not satisfy this requirement as I explained above.</li>
<li>The <code>__init__</code> of <code>C</code> has 2 parameters while those of <code>A</code> and <code>B</code> have only 1. You may be able to pass <code>a</code> to <code>A</code> but I don't know if there's a way for you to pass <code>b</code> to <code>B</code>.</li>
</ul>

<p>So it seems to me that <code>super</code> cannot help in this situation.</p>

python inheritance