Skip to main content
The 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

Always use @wraps for decorators:
Use @lru_cache for expensive pure functions:
Don’t cache functions with mutable arguments:

Complete Function List

  • @lru_cache(maxsize=128) - LRU cache with size limit
  • @cache - Unlimited cache (Python 3.9+)
  • @cached_property - Cache property value
  • @wraps(wrapped) - Update wrapper function
  • partial(func, /, *args, **keywords) - Partial application
  • partialmethod(func, /, *args, **keywords) - Partial for methods
  • reduce(function, iterable, initializer) - Cumulative application
  • @total_ordering - Fill in comparison methods
  • cmp_to_key(func) - Convert comparison function
  • @singledispatch - Single-dispatch generic function
  • @singledispatchmethod - Single-dispatch for methods

itertools

Iterator building blocks

operator

Function equivalents of operators

Built-in Functions

Core Python functions