On this page
weakref.ref
Prev: weakref.ProxyType | Up: weakref | Next: weakref.ReferenceType
Rel: weakref.CallableProxyType, weakref.finalize, weakref.getweakrefcount, weakref.getweakrefs
Ref: docs.python.org — weakref.ref
Tags: #public #python3 #stdlib #weakref
Import
import weakref
from weakref import ref
Signature & Overview
weakref.ref(...)
weakref.ref class.
Usage
import weakref
class Box: pass
b = Box()
r = weakref.ref(b)
assert r() is b
del b
assert r() is None
Gotchas
- Built-in
int,str,list,dict,tuplecannot be weakly referenced directly (custom classes andsetcan).
REPL Inspect
from weakref import ref
help(ref)
Linked from
Related notes
- tuts/python3/stdlib_module/weakref/ProxyType Linked here; links to this page; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/ReferenceType Linked here; links to this page; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/weakref/CallableProxyType Linked here; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/finalize Linked here; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/getweakrefcount Linked here; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/getweakrefs Linked here; shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/proxy shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/WeakKeyDictionary shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/WeakMethod shared tags: #stdlib, #weakref