Problem Statement
What is a safe use case for functools.lru_cache?
Explanation
lru_cache stores results for a function call based on arguments. It works best for pure functions where the same input always gives the same output.
Avoid caching when results depend on external state, time, or I O, unless you manage invalidation carefully.
Code Solution
SolutionRead Only
from functools import lru_cache
@lru_cache(maxsize=256)
def fib(n):
return n if n<2 else fib(n-1)+fib(n-2)