---
source: https://iinventedlight.com/library/notes/python-weakref.md
title: "Python weakref - caches and mappings that do not keep objects alive"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, weakref - Weak references; python/cpython (GitHub), Lib/weakref.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: weakref"
tags: [python, weakref, garbage-collection, caching, memory, stdlib]
client: Librarian
sources:
  - id: S1
    title: "weakref - Weak references"
    publisher: Python docs
    url: https://docs.python.org/3/library/weakref.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/weakref.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/weakref.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python weakref - caches and mappings that do not keep objects alive
**Answer:** For caches or mappings holding large objects, use the `WeakKeyDictionary` and `WeakValueDictionary` classes; register cleanup with `finalize`. A weak reference to an object is not enough to keep the object alive, so garbage collection can reclaim it and its entries in weak mappings are simply deleted.

## Examples

```python
import weakref

class Image:
    def __init__(self, name):
        self.name = name

cache = weakref.WeakValueDictionary()
img = Image("logo")
cache["logo"] = img
del img            # entry disappears once collected

other = Image("banner")
r = weakref.ref(other)
obj = r()          # None once the referent is gone

weakref.finalize(other, print, "cleaned up")
```

## Tools

| Class or function | Returns |
|---|---|
| `ref(obj[, callback])` | weak reference; call it to get the object, or `None` if no longer alive |
| `WeakValueDictionary` | mapping class that references values weakly |
| `WeakKeyDictionary` | mapping class that references keys weakly; attach data to objects owned elsewhere |
| `WeakSet` | set interface with weak references to its elements |
| `WeakMethod` | weak reference to a bound method |
| `finalize(obj, func, ...)` | callable finalizer called when obj is garbage collected |

## Rules

- Most programs only need the weak container types or `finalize`; creating your own weak references directly is not usually necessary.
- Not all objects can be weakly referenced. Class instances, Python functions, sets, generators and sockets can; `list` and `dict` need subclassing, and `tuple` and `int` do not support it even when subclassed (CPython).
- A plain `ref` to a bound method dies at once because a bound method is ephemeral; use `WeakMethod`.
- Weak references are hashable if the object is hashable.

## See also

- [python-collections-containers](https://iinventedlight.com/library/notes/python-collections-containers/)
- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-dataclasses](https://iinventedlight.com/library/notes/python-dataclasses/)

## Claims

- id: C1
  claim: A weak reference alone does not keep an object alive.
  source: S1
  evidence: "A weak reference to an object is not enough to keep the object alive"

- id: C2
  claim: Weak references are hashable if the object is hashable.
  source: S1
  evidence: "Weak references are hashable if the object is hashable."

- id: C3
  claim: WeakValueDictionary references values weakly.
  source: S2
  evidence: "Mapping class that references values weakly."

## Sources

- [S1] weakref - Weak references. Python docs. https://docs.python.org/3/library/weakref.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/weakref.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/weakref.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, weakref - Weak references; python/cpython (GitHub), Lib/weakref.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
