🐍 Python Intermediate

What is Python's functools module?

Answer

The functools module provides higher-order functions and tools for working with callables. lru_cache(maxsize=128): memoization decorator — caches function results based on input, O(1) lookup. wraps(wrapped): preserves the original function's metadata (name, docstring) when wrapping it in a decorator. partial(func, *args, **kwargs): creates a new function with some arguments pre-filled: double = partial(multiply, 2). reduce(func, iterable, initial): reduces an iterable to a single value by applying a function cumulatively. total_ordering: given __eq__ and one comparison method, fills in the rest. cached_property: like @property but caches the result on first access. singledispatch: function overloading based on the type of the first argument.