Python lru_cache: remember function results
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.
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):
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)
EOSCommand:
python3 s.pyRecorded output:
16 16 25
2
1 2Exit 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):
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)
EOSCommand:
python3 s.pyRecorded output:
3
0Exit 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):
cat > s.py <<'EOS'
from functools import lru_cache
@lru_cache(maxsize=None)
def f(x):
return x
f([1])
EOSCommand:
python3 s.pyRecorded 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
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.
All full guides: All guides