Functools
The `functools` module provides higher-order functions that act on or return other functions. These are essential tools for functional programming patterns in Python. The guide walks through partial & partialmethod, lru_cache & cache, reduce, singledispatch, wraps & Update Wrapper. `partial(func, *args, **kwargs)` creates a new function with some arguments pre-filled. Example: `base_two = partial(int, base=2)` creates a binary string parser. `partialmethod` works similarly for methods on classes, pre-filling method arguments. `@lru_cache(maxsize=128)` memoizes function results based on arguments, with a least-recently-used eviction policy. Set `maxsize=None` for unlimited cache. `@cache` (Python 3.9+) is shorthand for `@lru_cache(maxsize=None)`. Perfect for expensive, pure function calls. `reduce(func, iterable, initial)` applies a function cumulatively to items, reducing the iterable to a single value. Example: `reduce(operator.mul, range(1, 5))` computes 24. The `initial` value is used when the iterable is empty and as the first argument. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code.