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.

Level: intermediateTopic: Python advanced

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.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.