title: Calling parent class __init__ with multiple inheritance, what's the right way?
body: <p>Say I have a multiple inheritance scenario:</p>
  
  <pre><code>class A(object):
      # code for A here
  
  class B(object):
      # code for B here
  
  class C(A, B):
      def __init__(self):
          # What's the right code to write here to ensure 
          # A.__init__ and B.__init__ get called?
  </code></pre>
  
  <p>There's two typical approaches to writing <code>C</code>'s <code>__init__</code>:</p>
  
  <ol>
  <li>(old-style) <code>ParentClass.__init__(self)</code></li>
  <li>(newer-style) <code>super(DerivedClass, self).__init__()</code></li>
  </ol>
  
  <p>However, in either case, if the parent classes (<code>A</code> and <code>B</code>) <a href="http://fuhm.org/super-harmful/" rel="noreferrer">don't follow the same convention, then the code will not work correctly</a> (some may be missed, or get called multiple times).</p>
  
  <p>So what's the correct way again?  It's easy to say "just be consistent, follow one or the other", but if <code>A</code> or <code>B</code> are from a 3rd party library, what then?  Is there an approach that can ensure that all parent class constructors get called (and in the correct order, and only once)?</p>
  
  <p>Edit: to see what I mean, if I do:</p>
  
  <pre><code>class A(object):
      def __init__(self):
          print("Entering A")
          super(A, self).__init__()
          print("Leaving A")
  
  class B(object):
      def __init__(self):
          print("Entering B")
          super(B, self).__init__()
          print("Leaving B")
  
  class C(A, B):
      def __init__(self):
          print("Entering C")
          A.__init__(self)
          B.__init__(self)
          print("Leaving C")
  </code></pre>
  
  <p>Then I get:</p>
  
  <pre><code>Entering C
  Entering A
  Entering B
  Leaving B
  Leaving A
  Entering B
  Leaving B
  Leaving C
  </code></pre>
  
  <p>Note that <code>B</code>'s init gets called twice.  If I do: </p>
  
  <pre><code>class A(object):
      def __init__(self):
          print("Entering A")
          print("Leaving A")
  
  class B(object):
      def __init__(self):
          print("Entering B")
          super(B, self).__init__()
          print("Leaving B")
  
  class C(A, B):
      def __init__(self):
          print("Entering C")
          super(C, self).__init__()
          print("Leaving C")
  </code></pre>
  
  <p>Then I get: </p>
  
  <pre><code>Entering C
  Entering A
  Leaving A
  Leaving C
  </code></pre>
  
  <p>Note that <code>B</code>'s init never gets called.  So it seems that unless I know/control the init's of the classes I inherit from (<code>A</code> and <code>B</code>) I cannot make a safe choice for the class I'm writing (<code>C</code>).</p>