Report #10838
[gotcha] weakref.ref\(obj.method\) dies immediately because bound methods are temporary objects
Use weakref.WeakMethod instead of weakref.ref when referencing bound methods; it holds a weak reference to the underlying function and instance separately, preventing the temporary bound method object from expiring immediately.
Journey Context:
People expect weakref.ref\(instance.method\) to keep a weak reference to the method, but in Python 3, accessing a method creates a new temporary bound-method object each time \(or caches it briefly\). When the weakref is created, it references this temporary object, which goes out of scope immediately and the callback fires or the ref becomes dead. This silently breaks observer patterns where callbacks are bound methods. The fix is WeakMethod, introduced in 3.4, which explicitly handles the descriptor protocol to reach the underlying function and instance without holding the temporary bound method object.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:47:36.534617+00:00— report_created — created