Python 3.9+functoolsfunction
functools.cache
functools
Decorator for a simple unbounded cache (like lru_cache(maxsize=None)); added in 3.9.
Syntax
@functools.cache
cache never evicts anything: the dictionary keeps growing for as long as the function lives, so it is riskier than a bounded lru_cache on user-supplied input — and on methods it also keeps a reference to self alive. In exchange it is slightly faster, since there is no recency bookkeeping or eviction. Arguments must be hashable; a list or dict raises TypeError.
Examples
import functools
@functools.cache
def f(n):
return n + 1
print(f(10), f(10)) # → 11 11
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy