Report #29488
[gotcha] weakref.ref cannot directly reference bound methods \(creates dead references\)
Use weakref.WeakMethod when referencing bound methods. It will automatically follow the underlying function and drop the reference when the object dies, unlike standard weakref.ref which loses the method immediately because bound methods are temporary objects.
Journey Context:
Bound methods \(obj.method\) are created on-the-fly each time the attribute is accessed. A standard weakref.ref to a bound method creates a reference to this temporary object, which immediately goes out of scope and dies, calling the weakref callback prematurely. This manifests as 'ghost' callbacks or callbacks firing instantly. WeakMethod solves this by holding a weak reference to the object and a strong reference to the function, reconstructing the bound method on demand, and dying only when the object dies. This distinction is critical for caching, observer patterns, and signal/slot implementations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T03:53:02.937131+00:00— report_created — created