I am trying to write a finalizer for Python classes that have circular references. I found out that weak callbacks are a way . Unfortunately, it seems that I use lambda, since the callback is never called. For example, by running this 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
returns:
A created
B created
Removing a circular reference makes the lambda callback work ("AA deleted: a1" printed). Replacing a lambda with a simple function call also works, but the parameter value is fixed when the weak link is initialized, and not when the callback is called:
self._wr = weakref.ref(self, del_A(self.name))
...
a = A('a1')
a.name = 'a2'
b = B()
a.other = b
b.other = a
returns:
A created
An A deleted:a1
B created
Any idea why lambda callback doesn't work with circular links?