functoolsEasy Examples

Higher-order functions: lru_cache, partial, reduce, wraps, singledispatch

Getting started with functools

Basic import and usage of the functools module.

python
from functools import reduce, lru_cache

# reduce
total = reduce(lambda a, b: a + b, [1, 2, 3, 4, 5])
print(f"Sum: {total}")

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

print(f"fib(10): {fib(10)}")
print(f"fib(30): {fib(30)}")

The functools module is part of Python's standard library. Higher-order functions: lru_cache, partial, reduce, wraps, singledispatch.

Common functools operations

Frequently used functions from the functools module.

python
# More functools examples
import functools

print(f"functools module loaded successfully")
print(f"Location: {functools.__name__}")
print(f"Has {len(dir(functools))} attributes")

These are the most commonly used features of the functools module.

Want to try these examples interactively?

Open Easy Playground