all Python 3functoolsterm
functools
module
Higher-order functions: caching results, partial application, folding, decorator wrappers and dispatch on type.
Syntax
from functools import lru_cache, partial
functools is a toolkit for functions: lru_cache and cache remember results, partial fixes some arguments, wraps keeps the name and docstring of a decorated function. Ready-made iterators live next door, in itertools. The cache stores arguments as dictionary keys, so they must be hashable: a list argument raises TypeError.
Examples
from functools import lru_cache, reduce
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(80)) # → 23416728348467685
print(reduce(lambda a, b: a * b, [1, 2, 3, 4])) # → 24
See also
Open in the full glossarydocs.python.orgReport an inaccuracy