all Python 3weakreffunction
weakref.getweakrefs()
introspection
Returns a list of all weak references and proxies pointing at an object. Useful in debugging to see who is watching the object.
Syntax
weakref.getweakrefs(obj)
The list holds the reference objects themselves, not their owners: you cannot learn from it which variable or dict keeps the reference — gc.get_referrers does that. References with different callbacks are different objects, so each appears separately. For objects without weak-reference support the list is empty.
Examples
import weakref
class Item:
pass
it = Item()
r = weakref.ref(it)
watched = weakref.ref(it, lambda ref: None)
refs = weakref.getweakrefs(it)
print(len(refs), r in refs, watched in refs) # → 2 True True
print(weakref.getweakrefs('text')) # → []
See also
Open in the full glossarydocs.python.orgReport an inaccuracy