title: Monkey patch __del__ to new function
body: <p>For specific debugging purposes I'd like to wrap the <strong>del</strong> function of an arbitrary object to perform extra tasks like write the last value of the object to a file.</p>
  
  <p>Ideally I want to write
      monkey(x)
  and it should mean that the final value of x is printed when x is deleted</p>
  
  <p>Now I figured that <strong>del</strong> is a class method. So the following is a start:</p>
  
  <pre><code>class Test:
      def __str__(self):
          return "Test"
  
  def p(self):
      print(str(self))
  
  def monkey(x):
      x.__class__.__del__=p
  
  a=Test()
  monkey(a)
  del a
  </code></pre>
  
  <p>However if I want to monkey specific objects only I suppose I need to dynamically rewrite their class to a new one?! Moreover I need to do this anyway, since I cannot access <strong>del</strong> of built-in types?</p>
  
  <p>Anyone knows how to implement that?</p>