title: pytest mock a function call in __init__ of a class
body: <p>I have a class like this,</p>
  <pre><code>from external_package.module.sub_module import fun
  
  class MyClass:
      def __init__(self, x, y):
          self.x = x
          self.y = y
          self.z = fun()
          
      def printer(self):
          print(f&quot;X : {self.x}, y : {self.y}, z: {self.z}&quot;)
  </code></pre>
  <p>I want to write a unit test for this class and mock the function call that is happening in the <strong>init</strong> method.</p>
  <p>I have tried patching like this,</p>
  <pre><code>from external_package.module import sub_module
      
  def test_my_class(mocker):
  
      mocker.patch.object(sub_module, 'fun', return_value=&quot;this is mocked&quot;)
      obj = MyClass(10, 20)
      
      assert obj.z == &quot;this is mocked&quot;
  </code></pre>
  <p>But I can see that the call is not patched and a function call happens in the <strong>init</strong> method.</p>
  <p>How can I patch the call with pytest-mock or any other mocking packages?</p>