- weakref
Keep track of instances properly (weakref)
examples/oop/keep_track_properly.py
import weakref def prt(): print(list(Thing.things.keys())) class Thing: things = {} def __init__(self): Thing.things[id(self)] = weakref.ref(self) def __del__(self): print('__del__') del(Thing.things[id(self)]) def main(): prt() t1 = Thing() prt() t2 = Thing() prt() t3 = Thing() prt() t3 = None prt() main() prt()
[] [139793357111744] [139793357111744, 139793355980224] [139793357111744, 139793355980224, 139793355980080] __del__ [139793357111744, 139793355980224] __del__ __del__ []