all Python 3weakreffunction
weakref.getweakrefcount()
introspection
Returns how many weak references and proxies currently point at an object. Ordinary references are not counted — sys.getrefcount is for those.
Syntax
weakref.getweakrefcount(obj)
The number can be lower than expected: two calls of weakref.ref(obj) without a callback return the same reference object, so the count grows by 1, not 2. Proxies are counted separately. For an object that does not support weak references (int, list) the function just returns 0 rather than failing — it cannot tell you about support.
Examples
import weakref
class Item:
pass
it = Item()
r1 = weakref.ref(it)
r2 = weakref.ref(it)
print(r1 is r2, weakref.getweakrefcount(it)) # → True 1
p = weakref.proxy(it)
print(weakref.getweakrefcount(it)) # → 2
print(weakref.getweakrefcount([1, 2])) # → 0
See also
Open in the full glossarydocs.python.orgReport an inaccuracy