Learn functools

Higher-order functions: caching, partial application, decorators and dispatch

13 lessons · 2 hours · 4 levels · covers functools Python 3.13

functools provides tools that act on or return functions: partial application, reduce, memoisation with cache and lru_cache, cached_property, decorator helpers like wraps, total_ordering for comparisons, and single-dispatch generic functions. They make functional patterns idiomatic in Python and underpin many decorators and frameworks.

Before you start

functools builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 32 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 58 min in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 1 hour in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The functools tree

functoolsPre-filling arguments with partialPre-filling argumentswith partialFolding sequences with reduceFolding sequenceswith reduceMemoisation with cache and lru_cacheMemoisation withcache and lru_cacheSorting with key functions and cmp_to_keySorting with keyfunctions and cmp_…Writing decorators with wrapsWriting decoratorswith wrapscached_propertycached_propertyComparisons with total_orderingComparisons withtotal_ordering*args, **kwargs and parameter kinds*args, **kwargs andparameter kindsDefining functionsDefining functionsDecoratorsDecoratorsProperties and descriptorsProperties anddescriptorsInheritance, super() and compositionInheritance, super()and compositionThreads, processes and concurrent.futuresThreads, processesand concurrent.fut…
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Function tools

  1. Pre-filling arguments with partial · Essentials · 8 min

    Create a new callable with some arguments fixed using functools.partial.

  2. Folding sequences with reduce · Essentials · 7 min

    Combine all items of an iterable into one value with functools.reduce, and know when a built-in is clearer.

  3. Sorting with key functions and cmp_to_key · Essentials · 8 min

    Sort with key functions, and convert an old-style two-argument comparison into a key with cmp_to_key.

Caching

  1. Memoisation with cache and lru_cache · Essentials · 9 min

    Speed up pure functions by caching results with @cache and @lru_cache.

  2. cached_property · Practical · 9 min

    Compute an expensive attribute once per instance with @cached_property, and know how it differs from property plus cache.

  3. lru_cache in depth: methods, keys and memory · Advanced · 11 min

    Tune lru_cache with maxsize and typed, avoid caching on methods, and handle unhashable or mutable arguments.

Decorators and classes

  1. Writing decorators with wraps · Practical · 9 min

    Preserve a decorated function's name, docstring and signature with functools.wraps.

  2. Comparisons with total_ordering · Practical · 8 min

    Define __eq__ and one ordering method, and let @total_ordering fill in the rest.

Generic functions

  1. Generic functions with singledispatch · Advanced · 11 min

    Write a function whose implementation is chosen by the type of its first argument, with @singledispatch and register.

  2. partialmethod and singledispatchmethod · Advanced · 9 min

    Use the method-aware variants partialmethod and singledispatchmethod inside classes, and avoid the partial-as-method trap.

Internals

  1. update_wrapper and __wrapped__ · Expert · 11 min

    Understand what wraps actually does, how inspect and tools follow __wrapped__ chains, and how to write signature-changing decorators.

  2. Inside lru_cache: keys, locking and concurrency · Expert · 11 min

    How lru_cache builds keys and stores entries, its thread-safety guarantees, and why the same value may be computed twice.

  3. How singledispatch resolves types · Expert · 10 min

    Understand singledispatch's MRO-based resolution, ABC handling, the dispatch cache, and its interaction with typing.