functools: cache and partial

@functools.cache remembers each result by its arguments. Call the function again with the same arguments and you get the stored answer without running the body. This is memoization, and it turns the naive recursive Fibonacci from exponential time into linear time with one line.

@lru_cache(maxsize=128) does the same but keeps only the most recent results, so memory stays bounded. The arguments must be hashable, so you can't pass a list.

In interviews, @cache is the fastest way to turn a recursive solution into top-down dynamic programming. Say out loud what it costs: memory for every distinct argument. See the Algorithms challenges.

functools.partial(f, x) makes a new function with some arguments filled in. partial(int, base=2) is a function that parses binary strings.

Run it, then remove @cache and try fib(32) to feel the difference.