title: Weak reference callback is not called because of circular references
body: <p>I'm trying to write a finalizer for Python classes that have circular references. I found out that weak reference callbacks are the <a href="https://stackoverflow.com/questions/1935153/del-method-being-called-in-python-when-it-is-not-expected">way to go</a>. Unfortunately, it seems the lambda I use as a callback is never called. For example, running this code:</p>
  
  <pre><code>def del_A(name):
      print('An A deleted:' + name)
  
  class A(object):
      def __init__(self, name):
          print('A created')
          self.name = name
          self._wr = weakref.ref(self, lambda wr, n = self.name: del_A(n))
  
  class B(object):
      def __init__(self):
          print('B created')
  
  if __name__ == '__main__':
      a = A('a1')
      b = B()
      a.other = b
      b.other = a
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>A created
  B created
  </code></pre>
  
  <p>Removing the circular reference makes the lambda callback works ('An A deleted: a1' is printed). Replacing the lambda by a simple function call works too, but the parameter value is fixed when initializing the weak reference, and not when calling the callback:</p>
  
  <pre><code>self._wr = weakref.ref(self, del_A(self.name))
  ...
  a = A('a1')
  a.name = 'a2'
  b = B()
  a.other = b
  b.other = a
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>A created
  An A deleted:a1
  B created
  </code></pre>
  
  <p>Any idea why the lambda callback does not work with circular references?</p>