I would how activating a function to clean all my reference of this object in other structure when his variable is reassigned (so the object should be unreferenced everywhere )
rvc =ReferenceValueCluster()
rvc('key1')
wk = WeakrefDict(refval=rvc)
wk['key1'] =rvc('value1','value')
wk = WeakrefDict()
in this case wk is reassigned to a new WeakrefDict, I looking for a way to unreferencing this object everywhere.
I tried to figure it out with garbage collection and a custom call back at the start but gc is not activated after reassigned variable
import gc
import sys
class test():
def __init__(self) -> None:
self.text = "tejkejkej"
self.list = []
self.list.append(self)
def __del__(self)->None:
print('del test')
class test2():
def __init__(self) -> None:
self.list =[]
import copy
import inspect
def retrieve_name(var):
for fi in reversed(inspect.stack()):
names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
if len(names) > 0:
return names[0]
a =test()
b = a
w = test2()
w.list.append(a)
c = sys.getrefcount(a)
del c
l = dict()
def gb_callback_example(phase, info):
print(phase)
print(info["collected"])
if phase =='start':
temp = gc.get_referrers(a)[-1]
dic = copy.copy(temp)
print('-----------')
for k, v in dic.items():
print(retrieve_name(k))
print(retrieve_name(a))
print('----(((')
if type(v) == test and k != retrieve_name(a):
l[k] = v
print(l)
gc.callbacks.append(gb_callback_example)
a=0
w =[1,2,3]
when the variable a is reassigned to 0value , garbage collection is not activated