Memoize expensive calls
Recursive or repeated pure computations receive the same arguments.
Python · Module
Python’s functools module provides higher-order-function tools for adapting, decorating, caching, reducing, and dispatching callable objects. It includes utilities such as cache, lru_cache, partial, reduce, singledispatch, wraps, and update_wrapper.
Import functools utilities when a program needs to manipulate callable objects: decorators can preserve metadata, caches can reuse results, partial can pre-fill arguments, reduce can fold an iterable, and singledispatch can select an implementation by the first argument's type.
import functools
functools.cache(user_function)
functools.lru_cache(maxsize=128, typed=False)
functools.partial(func, /, *args, **keywords)
functools.reduce(function, iterable, /, initial=_initial_missing)
functools.singledispatch(func)
functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)| Name | Type | Description |
|---|---|---|
callable | callable | A callable accepted by the selected functools utility, such as the function passed to reduce, partial, cache, or a decorator. |
iterable_or_arguments | iterable / positional and keyword arguments | For reduce, an iterable whose elements are combined from left to right; for cache decorators, function arguments form the cache key. |
initial_or_maxsize | object / int or None | Optional starting accumulator for reduce, or the maximum number of recent entries retained by lru_cache. |
The module provides functions, decorators, and callable objects; individual APIs return wrapped callables, partial callables, reduced values, or dispatching generic functions as documented.
from functools import cache
@cache
def fibonacci(n: int) -> int:
if n < 2:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10))
print(fibonacci.cache_info())The decorator memoizes each result, so repeated recursive calls reuse cached values.
Recursive or repeated pure computations receive the same arguments.
A callback needs a fixed configuration value while retaining the callback interface.
Different input types need specialized behavior behind one public function.
A sequence must be accumulated into one value with a custom binary operation.
functools is a module for higher-order functions, meaning functions that act on or return other functions. Its utilities operate on callable objects generally, not only ordinary function definitions.
Follow-up: How would you preserve a decorated function's metadata? Use functools.wraps, which applies update_wrapper to the wrapper and copies the wrapped function's relevant attributes.
functools.cache creates a thin wrapper around a function and stores results for previously seen arguments. It is equivalent to an unbounded lru_cache and is useful when repeated calls have identical, hashable arguments.
Follow-up: When would lru_cache be preferable? Use it when maxsize should limit retained entries or when LRU statistics and cache management are needed.
partial returns a callable that behaves like the original function with positional and keyword arguments pre-filled. Arguments supplied later are appended positionally, while later keyword values extend or override the stored keywords.
Follow-up: How can dispatch be based on argument type instead? Decorate a base function with singledispatch and register implementations for supported types.
functools.cachefunctools.cached_propertyfunctools.lru_cachefunctools.partialfunctools.reducefunctools.singledispatchfunctools.singledispatchmethodfunctools.update_wrapper