# Python lru_cache: remember function results

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-lru-cache-full-en
`functools.lru_cache` stores results by arguments, so a repeated call with the same arguments does not run the function again. `cache_info()` shows hits and misses.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-88-A, TE-88-B and TE-88-C. The expected non-zero exit is TE-88-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-88-A: a repeated call is a cache hit and the function body runs once

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from functools import lru_cache
calls = []
@lru_cache(maxsize=None)
def sq(n):
    calls.append(n)
    return n * n
print(sq(4), sq(4), sq(5))
print(len(calls))
i = sq.cache_info()
print(i.hits, i.misses)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
16 16 25
2
1 2
```

Exit status: 0. The output matches the expected text written before the run.

### TE-88-B: with maxsize=1 the older result is dropped, and cache_clear empties the cache

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from functools import lru_cache
calls = []
@lru_cache(maxsize=1)
def f(n):
    calls.append(n)
    return n
f(1)
f(2)
f(1)
print(len(calls))
f.cache_clear()
print(f.cache_info().currsize)
EOS
```

Command:

```bash
python3 s.py
```

Recorded output:

```
3
0
```

Exit status: 0. This status and the output match what was written before the run.

### TE-88-C: expected non-zero exit: a cached function called with a list raises TypeError because a list is not hashable

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
cat > s.py <<'EOS'
from functools import lru_cache
@lru_cache(maxsize=None)
def f(x):
    return x
f([1])
EOS
```

Command:

```bash
python3 s.py
```

Recorded output: none, standard output was empty.

Standard error from the same run:

```
Traceback (most recent call last):
  File "/tmp/b22-ripmzne5/work/s.py", line 5, in <module>
    f([1])
TypeError: unhashable type: 'list'
```

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-88-B: with maxsize=1 the call f(1) was cached, f(2) replaced it, and the second f(1) ran the function again, so the function ran 3 times. After cache_clear() the cache size is 0. TE-88-C: calling the cached function with a list stopped with exit status 1. The last line of standard error is TypeError: unhashable type: 'list'.

## Limits

Arguments must be hashable (TE-88-C). TE-88-B runs `maxsize=1` and `cache_clear()` once each.

## Source

- https://docs.python.org/3/library/functools.html#functools.lru_cache

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-88-A` | a repeated call is a cache hit and the function body runs once | 0 | PASS | `210c3a12dfbf` |
| `TE-88-B` | with maxsize=1 the older result is dropped, and cache_clear empties the cache | 0 | PASS | `c1a1c3314ffb` |
| `TE-88-C` | expected non-zero exit: a cached function called with a list raises TypeError because a list is not hashable | 1 | PASS | `4678d76d267c` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Python str.join: build a string from a list](https://kairos-2z6s.onrender.com/res/python-str-join-full-en)
- [Python os.environ: read environment variables](https://kairos-2z6s.onrender.com/res/python-os-environ-full-en)
- [Python max and min with key: pick by a rule](https://kairos-2z6s.onrender.com/res/python-max-min-key-full-en)
- [Python Loop Else: runs when the loop did not break](https://kairos-2z6s.onrender.com/res/python-loop-else-full-en)
- [Python modules and imports](https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en)
