📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Functools

Functools

6 min read
@lru_cache caches function results to avoid repeated computation. partial() pre-fills function arguments. reduce() folds a list to a single value. @wraps preserves the original function's metadata in decorators.

functools Module

from functools import lru_cache, partial, reduce

# Memoization
@lru_cache(maxsize=None)
def fib(n):
    return n if n < 2 else fib(n-1) + fib(n-2)

print(fib(100))  # instant

# Partial application
double = partial(lambda x, y: x * y, 2)
print(double(5))  # 10

# Reduce
from functools import reduce
total = reduce(lambda a, b: a + b, [1,2,3,4,5])
print(total)  # 15