title: Removing the work from __init__ to aid unit testing
body: <p>The key to this question is aiding unit-testing. If I have a busy <code>__init__</code> (i.e. <code>__init__</code> that does complex initialization), I cannot simply instantiate an object of a class, but I need to mock/stub out all methods invoked on dependencies within the <code>__init__</code>.</p>
  
  <p>To illustrate this problem, here is example:</p>
  
  <pre><code>class SomeClass(object):
      def __init__(self, dep1, dep2, some_string):
          self._dep1 = dep1
          self._dep2 = dep2
          self._some_string = some_string
  
          # I would need to mock everything here (imagine some even more
          # complicated example)
          for dep2element in self._dep2:
              dep2element.set_dep(dep1)
          self._dep1.set_some_string(some_string)
  
      def fun1(self):
          ...
      def fun2(self):
          ...
      def fun3(self):
          ...
  </code></pre>
  
  <p>To test the <code>fun*</code> functions, every test must perform the complex construction.</p>
  
  <pre><code>class TestSomeClass(TestCase):
      def create_SomeClass(self, some_string):
          dep1 = Mock()
          # mock everything required by SomeClass' constructor
  
          dep2 = Mock()
          # mock everything required by SomeClass' constructor
  
          return SomeClass(dep1, dep2, some_string)
  
      def test_fun1(self):
          sc = self.create_SomeClass('some string')
          ...
  
      def test_fun2(self):
          sc = self.create_SomeClass('some other string')
          ...
  
      def test_fun3(self):
          sc = self.create_SomeClass('yet another string')
          ...
  </code></pre>
  
  <p>I find this redundant and would like to know how this problems can be elegantly handled in python, if not by moving the work from the constructor.</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>As @ecatmur suggested, to test some specific function, this code should do the trick:</p>
  
  <pre><code>def test_some_method():
      mobject = Mock(SomeClass)
      SomeClass.fun1(mobject)
  </code></pre>
  
  <p>With this approach all the methods will be mocked out. If <code>fun1</code> calls some other method you want executed (e.g. <code>fun2</code>) you can do it like this:</p>
  
  <pre><code>def test_some_method():
      mobject = Mock(SomeClass)
      mobject.fun2 = SomeClass.fun2.__get__(mobject)
      SomeClass.fun1(mobject)
  </code></pre>
  
  <p><code>SomeClass.fun2.__get__(mobject)</code> will produce <code>instancemethod</code> which will provide the correct binding.</p>
  
  <p><strong>¡Viva el Python!</strong></p>
  
  <p>ORIGINAL QUESTION:</p>
  
  <p>Original question was centered around moving the work done in <code>__init__</code> to the separate <code>init</code> method and different problems revolving that approach. My usual approach is to make this</p>
  
  <pre><code>class SomeClass(object):
      def __init__(self, dep1, dep2, some_string)
          self._dep1 = dep1
          self._dep2 = dep2
  
          # lots of mumbo-jumbo here...
  </code></pre>
  
  <p>become this</p>
  
  <pre><code>class SomeClass(object):
      def __init__(self, dep1, dep2)
          self._dep1 = dep1
          self._dep2 = dep2
  
      def initiate(self, some-string)
          # lots of mumto-jumbo here...
  </code></pre>
  
  <p>General sentiment was that moving work from <code>__init__</code> is not a common practice and would be meaningless to seasoned python developers.</p>