functools.lru_cache

A Python decorator from the functools module that memoises a function, remembering results for arguments it has seen.

In more detail

With maxsize=None the cache grows without limit; with a number it keeps only that many recent results and evicts the least recently used. Arguments must be hashable, because they become dict keys. Adding it to naive Fibonacci turns an exponential function into a linear one.

Example

@functools.lru_cache(maxsize=None)

Related terms

Taught in

References