import gc import time import weakref def collect(): gc.collect() time.sleep(0.15) weakrefs = [] sentinel = [] def watch(obj, kwarg=True): assert kwarg is True # log the death of the reference by appending to the sentinel ref = weakref.ref(obj, sentinel.append) weakrefs.append(ref) assert not sentinel print 1 thunk1 = lambda: None watch(thunk1) assert not sentinel del thunk1 collect() assert sentinel del sentinel[:] print 2 thunk2 = lambda: None watch(thunk2, kwarg=True) # <--- only difference: called with a kwarg assert not sentinel del thunk2 collect() assert sentinel