title: In Python, how to avoid calling __init__ twice in a class derived from a class with super() in its __new__:
body: <p>I am new to python. Somehow</p>
  
  <pre><code>__init__
  </code></pre>
  
  <p>is called twice for a class that is derived from another class using</p>
  
  <pre><code>super()
  </code></pre>
  
  <p>My question is how to avoid this because I have a very expensive computation there.</p>
  
  <pre><code>class A(object):
    def __new__(cls, *args, **kwargs):
      print("Class A: __new__")
      obj = super(A, cls).__new__(cls) # super is used here
      obj.__init__(*args, **kwargs)
      return obj
    def __init__(self, x):
      self.attrib = x+1
  
  class B(A):
    def __init__(self, x):
      print("Class B: __init__")
      self.prop = 2*x # some expensive computation
  
  a = A(10) # a test call
  
  b = B(20) # Q: here, how to avoid calling __init__ twice in class B?
  </code></pre>
  
  <p>Edit:
  Thank you both for answers. My real code is diagonalising a large sparse matrix using arpack built in scipy library. I am calling a class SpLuInv(LinearOperator) defined in arpack.py where class LinearOperator is defined in interface.py, both files are attached: <a href="https://github.com/scipy/scipy/blob/master/scipy/sparse/linalg/eigen/arpack/arpack.py" rel="noreferrer">arpack.py</a> and <a href="https://github.com/scipy/scipy/blob/master/scipy/sparse/linalg/interface.py" rel="noreferrer">interface.py</a>. When I call SpLuInv(), its <strong>init</strong> is called twice. From you answers, I think i need to remove obj.<strong>init</strong> that's in <strong>new</strong> of LinearOperator().</p>
  
  <p>Thank you Brendan Abel for your answer and Akshat Mahajan and Mike Graham for your comments. Removing</p>
  
  <pre><code>obj.__init__
  </code></pre>
  
  <p>from the</p>
  
  <pre><code>__new__
  </code></pre>
  
  <p>of </p>
  
  <pre><code>LinearOperator()
  </code></pre>
  
  <p>solved the problem. :)</p>