On this page
weakref.WeakValueDictionary
Prev: weakref.WeakSet | Up: weakref
Rel: weakref.CallableProxyType, weakref.finalize, weakref.getweakrefcount, weakref.getweakrefs
Ref: docs.python.org — weakref.WeakValueDictionary
Tags: #public #python3 #stdlib #weakref
Import
import weakref
from weakref import WeakValueDictionary
Signature & Overview
weakref.WeakValueDictionary(other=(), /, **kw)
Mapping class that references values weakly.
Methods & Attributes
WeakValueDictionary.clear(self)— D.clear() -> None. Remove all items from D.WeakValueDictionary.copy(self)—WeakValueDictionary.copymethod/property.WeakValueDictionary.get(self, key, default=None)—WeakValueDictionary.getmethod/property.WeakValueDictionary.items(self)—WeakValueDictionary.itemsmethod/property.WeakValueDictionary.itervaluerefs(self)— Return an iterator that yields the weak references to the values.WeakValueDictionary.keys(self)—WeakValueDictionary.keysmethod/property.WeakValueDictionary.pop(self, key, *args)—WeakValueDictionary.popmethod/property.WeakValueDictionary.popitem(self)—WeakValueDictionary.popitemmethod/property.WeakValueDictionary.setdefault(self, key, default=None)—WeakValueDictionary.setdefaultmethod/property.WeakValueDictionary.update(self, other=None, /, **kwargs)—WeakValueDictionary.updatemethod/property.WeakValueDictionary.valuerefs(self)— Return a list of weak references to the values.WeakValueDictionary.values(self)—WeakValueDictionary.valuesmethod/property.
Usage
import weakref
class CacheObj: pass
obj = CacheObj()
cache = weakref.WeakValueDictionary({'k': obj})
assert cache['k'] is obj
del obj
assert 'k' not in cache
Gotchas
- Ideal for object caches: Entries vanish automatically once no external caller holds a strong reference.
REPL Inspect
from weakref import WeakValueDictionary
help(WeakValueDictionary)
Linked from
Related notes
- tuts/python3/stdlib_module/weakref/WeakSet 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/ProxyType shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/ref shared tags: #stdlib, #weakref
- tuts/python3/stdlib_module/weakref/ReferenceType shared tags: #stdlib, #weakref