all Python 3weakreffunction
weakref.proxy()
weak references
Creates a proxy — a weak reference you use as if it were the object itself, without calling r(). Once the object is gone, any use raises ReferenceError.
Syntax
p = weakref.proxy(obj, callback=None)
A proxy is handier than ref but riskier: ref honestly returns None, while a proxy fails in the middle of an expression with ReferenceError. A proxy is unhashable, so it cannot be a dict key or a set element, and proxy is obj is always False. A callable object yields a CallableProxyType, anything else a ProxyType. If you need to check whether it is still alive, use weakref.ref.
Examples
import weakref
class User:
name = 'Ann'
u = User()
p = weakref.proxy(u)
print(p.name) # → Ann
del u
try:
p.name
except ReferenceError:
print('ReferenceError') # → ReferenceError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy