title: I don't understand this python __del__ behaviour
body: <p>Can someone explain why the following code behaves the way it does:</p>
  
  <pre><code>import types
  
  class Dummy():
      def __init__(self, name):
          self.name = name
      def __del__(self):
          print "delete",self.name
  
  d1 = Dummy("d1")
  del d1
  d1 = None
  print "after d1"
  
  d2 = Dummy("d2")
  def func(self):
      print "func called"
  d2.func = types.MethodType(func, d2)
  d2.func()
  del d2
  d2 = None
  print "after d2"
  
  d3 = Dummy("d3")
  def func(self):
      print "func called"
  d3.func = types.MethodType(func, d3)
  d3.func()
  d3.func = None
  del d3
  d3 = None
  print "after d3"
  </code></pre>
  
  <p>The output (note that the destructor for d2 is never called) is this (python 2.7)</p>
  
  <pre><code>delete d1
  after d1
  func called
  after d2
  func called
  delete d3
  after d3
  </code></pre>
  
  <p>Is there a way to "fix" the code so the destructor is called without deleting the method added?  I mean, the best place to put the d2.func = None would be in the destructor!</p>
  
  <p>Thanks</p>
  
  <p>[edit] Based on the first few answers, I'd like to clarify that I'm not asking about the merits (or lack thereof) of using <code>__del__</code>.  I tried to create the shortest function that would demonstrate what I consider to be non-intuitive behavior.  I'm assuming a circular reference has been created, but I'm not sure why.  If possible, I'd like to know how to avoid the circular reference....</p>