functools module provides higher-order functions and operations on callable objects.
Module Import
Caching Decorators
@lru_cache - LRU Cache
Least Recently Used cache decorator for memoization.@cache - Unlimited Cache
Simple unbounded cache (Python 3.9+).Function Wrappers
@wraps - Preserve Function Metadata
Preserves original function’s metadata when creating wrappers.Partial Functions
partial() - Partial Application
Create new function with some arguments pre-filled.partialmethod() - Partial for Methods
Function Composition
reduce() - Reduce Iterable
Apply function cumulatively to items.Comparison Functions
@total_ordering - Complete Comparison Methods
Automatically provides all comparison methods from a subset.cmp_to_key() - Convert Comparison Function
Convert old-style comparison function to key function.Single Dispatch
@singledispatch - Generic Functions
Create generic functions that behave differently based on argument type.@singledispatchmethod - For Class Methods
Cached Properties
@cached_property - Cache Property Value
Practical Examples
Memoization for Expensive Functions
Retry Decorator
Function Pipeline
Timing Decorator
Currying with partial
Best Practices
Complete Function List
Caching
Caching
@lru_cache(maxsize=128)- LRU cache with size limit@cache- Unlimited cache (Python 3.9+)@cached_property- Cache property value
Function Tools
Function Tools
@wraps(wrapped)- Update wrapper functionpartial(func, /, *args, **keywords)- Partial applicationpartialmethod(func, /, *args, **keywords)- Partial for methodsreduce(function, iterable, initializer)- Cumulative application
Comparison
Comparison
@total_ordering- Fill in comparison methodscmp_to_key(func)- Convert comparison function
Dispatch
Dispatch
@singledispatch- Single-dispatch generic function@singledispatchmethod- Single-dispatch for methods
Related Modules
itertools
Iterator building blocks
operator
Function equivalents of operators
Built-in Functions
Core Python functions
