title: Python: Monkeypatching a method of an object
body: <p>I'm hitting an API in python through requests' Session class. I'm making GET &amp; POST method call using requests.Session().</p>
  
  <p>On every call(GET/POST) failure, I want to notify another process. I can do this by creating a utility method as follows:</p>
  
  <pre><code>s = request.Session()
  def post():
      try:
          s.post(URL,data,headers)
      except:
          notify_another_process()
  </code></pre>
  
  <p>And call this method instead of <code>requests.Session().post</code> directly. </p>
  
  <p>But, I want to monkeypatch this code to <code>requests.Session().post</code> and want the additional functionality of notifying the other process in the <code>requests.Session().post</code> method call itself. How can I achieve this?</p>
  
  <p><strong>EDIT 1 :</strong></p>
  
  <p>requests.Session()'s post method has the following signature:</p>
  
  <pre><code>def post(self, url, data=None, json=None, **kwargs):
      return self.request('POST', url, data=data, json=json, **kwargs)
  </code></pre>
  
  <p>If I somehow try to make my a custom post like the following:</p>
  
  <pre><code>def post_new(self, url, data=None, json=None, **kwargs):
      try:
          s.post(url,data, json,kwargs)
      except:
          notify_another_process()
  </code></pre>
  
  <p>and do a patch as follows:</p>
  
  <pre><code>requests.post = post_new
  </code></pre>
  
  <p>This isn't really a good monkeypatching because I'm not using <code>self</code> but <code>session's object</code> inside <code>session.post</code>.</p>