1. Use functools.cache for unbounded memoization

If you want an unbounded cache, prefer functools.cache over lru_cache(maxsize=None).

The name matches the intent directly: you want memoization, not an LRU policy with an unlimited size disguised through a special argument. That small naming improvement makes maintenance easier because readers do not have to decode the configuration to understand the behavior.

Note

Python 3.9+

1.1. Don’t do this

1from functools import lru_cache
2
3@lru_cache(maxsize=None)
4def fib(n):
5    if n < 2:
6        return n
7    return fib(n - 1) + fib(n - 2)

1.2. Do this

1from functools import cache
2
3@cache
4def fib(n):
5    if n < 2:
6        return n
7    return fib(n - 1) + fib(n - 2)