weakref.WeakValueDictionary — A mapping class that references values weakly. Entries are discarded when no strong reference to the value exists.
| Use Case | Command | Description |
|---|---|---|
| Create a weak-value dictionary | WeakValueDictionary() | Empty dictionary, or initialize with other mapping/iterable |
| Get value by key | d[key] | Returns the value, raises KeyError if missing |
| Set value | d[key] = value | Stores a weak reference to value |
| Delete entry | del d[key] | Removes the key-value pair |
| Check membership | key in d | Returns True if key exists |
| Get with default | d.get(key, default=None) | Return value or default if missing |
| Iterate keys | iter(d) or d.keys() | Returns an iterator over keys |
| Iterate values | d.values() | Returns a view of values (weak references may be dead) |
| Iterate items | d.items() | Returns a view of key-value pairs |
| Pop item | d.pop(key, *args) | Remove and return value; raises KeyError if missing and no default |
| Pop arbitrary item | d.popitem() | Remove and return (key, value) pair; raises KeyError if empty |
| Set default | d.setdefault(key, default=None) | Return value if key exists, else set and return default |
| Update from another mapping | d.update(other, **kwargs) | Merge keys/values from other and keyword arguments |
| Copy | d.copy() | Returns a shallow copy (values are still weak references) |
| Clear all entries | d.clear() | Remove all items (inherited from MutableMapping) |
| Get weak refs to values | d.valuerefs() | Returns a list of weak reference objects to values |
| Iterate weak refs to values | d.itervaluerefs() | Returns an iterator of weak reference objects |
Mapping class that references values weakly. Entries in the dictionary will be discarded when no strong reference to the value exists anymore.
WeakValueDictionarycollections.abc.MutableMappingcollections.abc.Mappingcollections.abc.Collectioncollections.abc.Sizedcollections.abc.Iterablecollections.abc.Containerbuiltins.object__contains__(self, key)__copy__ = copy(self)__deepcopy__(self, memo)__delitem__(self, key)__getitem__(self, key)__init__(self, other=(), /, **kw) — Initialize self. See help(type(self)) for accurate signature.__ior__(self, other)__iter__ = keys(self)__len__(self)__or__(self, other) — Return self|value.__repr__(self) — Return repr(self).__ror__(self, other) — Return value|self.__setitem__(self, key, value)copy(self)get(self, key, default=None) — D.get(k[,d]) → D[k] if k in D, else d. d defaults to None.items(self) — D.items() → a set-like object providing a view on D's itemsitervaluerefs(self) — Return an iterator that yields the weak references to the values. The references are not guaranteed to be 'live' at the time they are used, so the result of calling the references needs to be checked before being used. This can be used to avoid creating references that will cause the garbage collector to keep the values around longer than needed.keys(self) — D.keys() → a set-like object providing a view on D's keyspop(self, key, *args) — D.pop(k[,d]) → v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised.popitem(self) — D.popitem() → (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.setdefault(self, key, default=None) — D.setdefault(k[,d]) → D.get(k,d), also set D[k]=d if k not in Dupdate(self, other=None, /, **kwargs) — D.update([E, ]**F) → None. Update D from mapping/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k]. If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v. In either case, this is followed by: for k, v in F.items(): D[k] = v.valuerefs(self) — Return a list of weak references to the values. The references are not guaranteed to be 'live' at the time they are used, so the result of calling the references needs to be checked before being used. This can be used to avoid creating references that will cause the garbage collector to keep the values around longer than needed.values(self) — D.values() → an object providing a view on D's values__dict__ — dictionary for instance variables (if defined)__weakref__ — list of weak references to the object (if defined)__abstractmethods__ = frozenset()collections.abc.MutableMappingclear(self) — D.clear() → None. Remove all items from D.collections.abc.Mapping__eq__(self, other) — Return self==value.collections.abc.Mapping__hash__ = None__reversed__ = Nonecollections.abc.Collection__subclasshook__(C) from abc.ABCMeta — Abstract classes can override this to customize issubclass(). This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).collections.abc.Iterable__class_getitem__ = GenericAlias(...) from abc.ABCMeta — Represent a PEP 585 generic type. E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).Generated by phpman v4.9.27-5-g2cb901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-20 04:13 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format