import weakref class Obj: pass def wr_callback(obj): raise ValueError(42) obj = Obj() ref = weakref.ref(obj, wr_callback) obj = None ref = None