I have a class that stores all created references in a static list like this:
class A:
_List = []
def __init__(self):
A._List.append(self)
def __del__(self):
A._List.remove(self)
Now if I do the following:
a = A()
del a
a is not deleted, since it is still referenced to in the list. But the reference would be destroyed if the destructor would be called. Is there a way to somehow force python to execute the __del__ method anyway, so that it can get removed from the static list?
You can use
weakref.WeakValueDictionary(orweakref.WeakKeyDictionary), it would automatcally remove any elements that do not exist anymore: