AI engineering glossary · Python advanced
What is functools.lru_cache?
functools.lru_cache (also called lru_cache): A decorator that remembers the results of a function for previously seen arguments, evicting the least recently used ones when full. It speeds up pure functions with repeated inputs, such as recursive calculations.
functools.lru_cache: an example
@lru_cache(maxsize=None) on a recursive fib(n) makes fib(50) fast because each value is computed once.
Related terms
More Python advanced terms
Where you learn it
functools.lru_cache is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.