1. Home
  2. Blog
  3. Software Development

Software Development

Python functools Explained: lru_cache, partial, reduce, wraps and More, Tested

Python functools is the standard library toolbox for working with functions: caching results, pre-filling arguments, folding lists, writing proper decorators and dispatching on type. Here is what each tool does, with output we ran on Python 3.13.

9 min read
Python functools explained: lru_cache, partial, reduce and wraps

Key takeaways

  • Python functools is built in; the most useful tools are lru_cache, partial, wraps and cached_property.
  • Caching a recursive Fibonacci cut fib(30) from 0.106 seconds to 0.137 milliseconds in our run.
  • Without @wraps, our decorated function reported its name as inner and lost its docstring.
  • Cached functions need hashable arguments: passing a list raised TypeError.

Sooner or later every Python developer writes a slow function that gets called with the same inputs, a decorator that breaks debugging, or a lambda whose only job is to fix one argument. The Python functools module solves all three with a few well-tested tools. You do not need to install anything; it ships with every Python version you are likely to use.

All examples here were run on Python 3.13.6 and the printed output is copied from that run. Timings depend on hardware, so focus on the ratios. Decorators come up a lot below, so if they are new to you, start with our Python decorators guide.

What is functools?

functools is a standard library module for higher-order functions: functions that take or return other functions. It groups tools into three families:

  • Caching: lru_cache, cache and cached_property remember results.
  • Building functions: partial, reduce and singledispatch create or combine behaviour.
  • Helpers for classes and decorators: wraps and total_ordering fill in boilerplate.

lru_cache and cache: memoize slow functions

lru_cache stores the result for each set of arguments. The next call with the same arguments returns the stored value instead of recomputing it. Recursive functions with overlapping sub-problems benefit the most:

from functools import lru_cache

def fib_plain(n):
    return n if n < 2 else fib_plain(n - 1) + fib_plain(n - 2)

@lru_cache(maxsize=None)
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

fib(30)
print(fib.cache_info())
fib(30) plain 0.106s cached 0.137ms
CacheInfo(hits=28, misses=31, maxsize=None, currsize=31)

The plain version makes over a million calls for n = 30. The cached one computed each value from 0 to 30 once (31 misses) and served the rest from the cache (28 hits). That turned roughly a tenth of a second into well under a millisecond.

Since Python 3.9, @cache is a shorter spelling of @lru_cache(maxsize=None): an unbounded cache with slightly less overhead. Use cache_info() to check hit rates and cache_clear() to empty the cache, for example between tests.

How maxsize really works

LRU stands for “least recently used”. When a bounded cache is full, the entry that was used longest ago is evicted. We set maxsize=2 and called a counting function with the inputs 1, 2, 1, 3, 1, 2:

@lru_cache(maxsize=2)
def sq(x):
    global calls
    calls += 1
    return x * x

for x in [1, 2, 1, 3, 1, 2]:
    sq(x)
print(calls, sq.cache_info())
4 CacheInfo(hits=2, misses=4, maxsize=2, currsize=2)

Walk through it: 1 and 2 are misses. The second 1 is a hit. Then 3 is a miss and evicts 2, the least recently used. The third 1 is a hit, and the final 2 is a miss again because it was evicted. Choose maxsize from real usage; a cache that is too small keeps throwing away exactly what you need next.

Tip: only cache pure functions, whose result depends only on their arguments. Caching a function that reads the clock, a database or a file will return stale data.

partial: pre-fill arguments

partial creates a new callable with some arguments already fixed. It is clearer than a one-line lambda and easier to inspect:

from functools import partial

pow2 = partial(pow, exp=2)
print(pow2(9))                      # 81

int_bin = partial(int, base=2)
print(int_bin("1011"))              # 11
print(int_bin.func.__name__, int_bin.keywords)   # int {'base': 2}

The func, args and keywords attributes let you see what was fixed, which a lambda hides. Typical uses are callbacks for GUI buttons, pre-configured loggers and passing a configured function to map or a thread pool. If you prefer lambdas for one-off cases, our Python lambda functions guide compares the two styles.

reduce: fold a sequence into one value

reduce applies a two-argument function cumulatively from left to right. It moved from the built-ins into functools in Python 3:

from functools import reduce
import operator

print(reduce(operator.mul, [1, 2, 3, 4, 5]))   # 120
print(reduce(operator.add, [], 0))             # 0
reduce(operator.add, [])
TypeError: reduce() of empty iterable with no initial value

Always pass an initial value when the input could be empty. And check for a built-in first: sum, max, min, any, all and math.prod are clearer than reduce for the common cases. Keep reduce for genuinely custom folds, such as merging a list of dictionaries.

wraps: decorators that keep their identity

A decorator replaces a function with a wrapper. Without help, the wrapper’s name and docstring replace the original’s, which breaks logs, documentation and test reports. We decorated the same function with and without @wraps:

from functools import wraps

def nowrap(f):
    def inner(*a, **k):
        return f(*a, **k)
    return inner

def withwrap(f):
    @wraps(f)
    def inner(*a, **k):
        return f(*a, **k)
    return inner
no wraps: inner None
wraps: greet2 Say hello. greet2

With @wraps(f), the wrapper copies __name__, __doc__, __module__ and more, and adds __wrapped__ so tools can reach the original. Put it in every decorator you write; there is no downside.

cached_property: compute once per object

cached_property turns a method into an attribute that is computed on first access and then stored on the instance:

from functools import cached_property

class Report:
    def __init__(self, rows):
        self.rows, self.n = rows, 0
    @cached_property
    def total(self):
        self.n += 1
        return sum(self.rows)

r = Report([1, 2, 3])
r.total; r.total
print(r.n, r.total)    # 1 6

The property body ran once even though we read it three times. Because the value is stored in the instance dictionary, deleting it with del r.total forces a recompute. It suits expensive values that do not change during an object’s life, such as a parsed config. For more on classes, see our Python classes and objects guide.

singledispatch: one function, many types

singledispatch picks an implementation based on the type of the first argument, which replaces long isinstance chains:

from functools import singledispatch

@singledispatch
def fmt(x):
    return f"obj:{x}"

@fmt.register
def _(x: int):
    return f"int:{x:,}"

@fmt.register
def _(x: list):
    return "list:" + ",".join(map(fmt, x))

print(fmt(1234567), fmt([1, "a"]), fmt(2.5))
int:1,234,567 list:int:1,obj:a obj:2.5

Registration reads the type hint, so the code stays tidy. New types can be added from other modules without touching the original function. For methods, use singledispatchmethod. Type hints in general are covered in our Python type hints guide.

total_ordering: fewer comparison methods

Define __eq__ and one of __lt__, __le__, __gt__ or __ge__, and @total_ordering fills in the rest. In our test, a version class with only __eq__ and __lt__ answered Ver(1,2) >= Ver(1,1) with True and Ver(1,2) <= Ver(1,1) with False. The generated methods are a little slower than hand-written ones, so skip it in very hot code.

Pitfalls to avoid

  • Unhashable arguments. Cached functions build a key from the arguments. Calling a cached function with a list raised TypeError: unhashable type: 'list' in our run. Convert to a tuple first.
  • Caching methods. lru_cache on an instance method includes self in the key and keeps every instance alive as long as the cache does. Prefer cached_property or a module-level cached function.
  • Unbounded memory. @cache never evicts. For inputs from users or the network, set a maxsize.
  • Mutable results. A cached list or dict is shared by every caller. If one caller changes it, everyone sees the change.
  • Stale caches in tests. Call cache_clear() in a fixture so one test cannot leak results into another.

Python functools cheat sheet

Tool Use it to Since
lru_cache Memoize with a size limit 3.2
cache Memoize without a limit 3.9
cached_property Compute an attribute once per object 3.8
partial Fix some arguments of a function 2.5
reduce Fold a sequence into one value moved in 3.0
wraps Keep metadata in decorators 2.5
singledispatch Choose an implementation by type 3.4
total_ordering Generate comparison methods 2.7

Final thoughts

Python functools rewards a small investment. Add @wraps to every decorator, reach for lru_cache or cache when a pure function repeats work, and replace argument-fixing lambdas with partial. Our measurements showed the payoff clearly: one decorator line made a recursive function several hundred times faster. Pair these tools with Python generators and you will write shorter, faster, more readable code.

Frequently asked questions

What is the difference between lru_cache and cache?

cache is an unbounded cache, the same as lru_cache with maxsize set to None. lru_cache accepts a maxsize and evicts the least recently used entry when it is full.

Why does lru_cache raise TypeError: unhashable type?

The cache uses the arguments as a dictionary key, so they must be hashable. Lists, dicts and sets are not; convert them to tuples or frozensets before calling.

Why should I use functools.wraps in decorators?

Without it, the decorated function takes the wrapper’s name and loses its docstring, which confuses logs, help text and test reports. wraps copies that metadata across.

Is functools.partial better than a lambda?

For fixing arguments, usually yes. A partial object is clearer, can be pickled when its function can, and exposes func, args and keywords for inspection.

Is reduce still recommended in Python 3?

Use it sparingly. Built-ins such as sum, max, min, any, all and math.prod are clearer for common cases; keep reduce for custom folds and always give an initial value.

Keep learning

Work with Stack Quora

Need help with testing, WordPress or SEO?

We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.

Keep reading

More guides on testing, AI & modern development

Enjoyed this article? Pick a topic and jump straight into our newest tutorials.

Artificial Intelligence

BM25 Explained: How Keyword Search Ranks Results, With Python Code and Hybrid Search

BM25 explained with a pure Python implementation we ran: the formula, IDF, term saturation, the k1 and b parameters, why…

Read article →
Software Development

HTTP Methods Explained: GET, POST, PUT, PATCH, DELETE, Tested on a Live Server

HTTP methods explained with a small server we ran: GET, POST, PUT, PATCH, DELETE, HEAD and OPTIONS, safe vs idempotent,…

Read article →
DevOps

Git Bisect Explained: Find the Commit That Broke Your Code, Manually or Automatically

Git bisect explained with a tested repository: find the one bad commit among 41 in 5 test runs, automate it…

Read article →
Test Automation

pytest Markers Explained: Custom Marks, skip, skipif, xfail and -m Filters

pytest markers explained with a tested suite: register custom marks, run only smoke tests with -m, combine filters with and,…

Read article →
Software Testing

Positive vs Negative Testing: Differences, 24 Real Test Cases and a Checklist

Positive vs negative testing explained with a real signup form: 6 positive and 18 negative test cases run in pytest,…

Read article →
Databases & SQL

SQL ORDER BY Multiple Columns: ASC, DESC, NULLS LAST, Case and Custom Order

SQL ORDER BY with multiple columns explained with tested queries: mixed ASC and DESC, tie-breakers, NULLS FIRST and LAST, case-insensitive…

Read article →
Databases & SQL

SQL Running Total: SUM OVER, Per-Group Totals, Moving Sums and the RANGE Trap

How to calculate a SQL running total with SUM() OVER: per-group totals, daily cumulative sums, moving sums, percent of total,…

Read article →
Software Development

JavaScript Sort Array: Numbers, Objects, Strings and Why 10 Comes Before 5

How to sort an array in JavaScript correctly: numbers with a compare function, objects by one or more fields, strings…

Read article →
Software Development

Python String Methods: Cheat Sheet, Real Examples and the strip() Trap

Python string methods with tested examples: split, join, strip, replace, find, startswith, is-methods, casefold, partition and translate, plus the strip()…

Read article →
Software Development

Python zip() Explained: Loop Two Lists at Once, Unzip, strict=True and Traps

Python zip() pairs items from several lists in one loop. Tested examples for dicts, unzipping with *, strict=True, zip_longest, sliding…

Read article →
Artificial Intelligence

Cosine Similarity Explained: Formula, Worked Example and Python Code

Cosine similarity explained simply: the formula, a worked example by hand, pure Python and NumPy code, cosine vs Euclidean distance…

Read article →
Software Development

UTF-8 Encoding Explained: How Text Becomes Bytes and How to Fix Garbled Text

UTF-8 encoding explained with byte-level examples we ran in Python: how characters become 1 to 4 bytes, why café turns…

Read article →
DevOps

Git Squash Commits: 4 Easy Ways, Tested Step by Step (rebase -i, reset, merge)

Git squash commits explained with 4 tested methods: interactive rebase, reset –soft, merge –squash and fixup with autosquash. Before-and-after logs,…

Read article →
Test Automation

pytest parametrize Explained: 7 Patterns with Tested Examples

pytest parametrize explained with 7 tested patterns: basic cases, readable ids, stacked decorators, pytest.param with xfail and skip, indirect fixtures…

Read article →
Software Testing

Retesting vs Regression Testing: 8 Key Differences with a Real Bug Fix Example

Retesting vs regression testing explained with a real bug fix run in pytest: retesting confirmed the fix, the regression run…

Read article →
Databases & SQL

Delete Duplicate Rows in SQL: 5 Safe Methods, Tested Step by Step

How to delete duplicate rows in SQL safely: find them, preview them, then remove them with ROW_NUMBER, MIN(id), EXISTS, rowid…

Read article →
Databases & SQL

SQL RANK vs DENSE_RANK vs ROW_NUMBER: Differences with Tested Examples

SQL RANK vs DENSE_RANK vs ROW_NUMBER in one table: how each handles ties, top-N per group, the second-highest salary trap…

Read article →
Software Development

JavaScript Optional Chaining (?.) Explained: 8 Examples, ?? Defaults and Traps

JavaScript optional chaining stops “Cannot read properties of undefined” errors. 8 tested examples for properties, arrays and methods, ?? vs…

Read article →
Artificial Intelligence

RAG Chunking Strategies Explained: Fixed, Overlap, Sentence, Recursive and Headings, Measured

RAG chunking strategies compared on one document with tiktoken 0.14: fixed-size, overlap, sentence packing, recursive splitting and heading-based chunks, measuring…

Read article →
DevOps

Git Stash Explained: Save, Apply, Pop and Untracked Files, Tested

Git stash explained with commands we ran on Git 2.56: push with a message, untracked files, list and show, apply…

Read article →
Software Testing

Pairwise Testing Explained: How All-Pairs Cuts 216 Tests to 13, Tested

Pairwise testing explained with a generator we ran in Python 3.13: a checkout with 216 combinations covered by 13 tests,…

Read article →
Test Automation

Test Doubles Explained: Dummy, Stub, Spy, Mock and Fake, with Tested Python Code

Test doubles explained with one Python example run in pytest 9.2: dummy, stub, spy, mock and fake, when to use…

Read article →
Databases & SQL

SQL COALESCE Explained: Defaults, NULLIF, IFNULL vs ISNULL and Traps, Tested

SQL COALESCE explained with queries we ran in SQLite 3.50: first non-null values, empty strings, NULL in maths, NULLIF for…

Read article →
Databases & SQL

SQL Self Join Explained: Employee-Manager, Pairs and Row Comparisons, Tested

SQL self join explained with queries we ran in SQLite 3.50: employee and manager, earning more than the manager, unique…

Read article →
Artificial Intelligence

LLM Context Window Explained: Tokens, Limits and How to Fit More, Measured

The LLM context window explained with token counts we measured using tiktoken: words vs tokens for prose, code and Hindi,…

Read article →
DevOps

Git Cherry-Pick Explained: Copy the Commit You Need, Safely, Tested

Git cherry-pick explained with commands we ran in Git 2.56: copying a hotfix to a release branch, new hashes and…

Read article →
Test Automation

Snapshot Testing Explained: How It Works, When It Helps and When It Hurts

Snapshot testing explained with a pytest and syrupy demo we ran: creating snapshots, reading the diff after a code change,…

Read article →
Software Testing

Mutation Testing Explained: Why 100% Coverage Still Missed Our Bug

Mutation testing explained with a small mutation tool we built and ran: 14 mutants, a test suite with 100% line…

Read article →
Artificial Intelligence

Prompt Injection Explained: How AI Apps Get Tricked and How to Defend Them

Prompt injection explained for builders: direct and indirect attacks, why keyword filters fail (our test blocked 1 of 4), and…

Read article →
DevOps

DNS Records Explained: A, AAAA, CNAME, MX, TXT and TTL With Real Lookups

DNS records explained with lookups we ran: how resolution works, A and AAAA, CNAME rules, MX and null MX, TXT…

Read article →
DevOps

Feature Flags Explained: Safe Releases, Gradual Rollouts and Flag Hygiene

Feature flags explained with a rollout simulation we ran: flag types, percentage rollouts with stable hashing, kill switches, allow lists,…

Read article →
Test Automation

Test Data Generation With Python Faker: Realistic, Repeatable and Safe Data

Test data generation with Python Faker, run and measured: seeding for repeatable data, locales, unique values, domain-shaped records, edge cases…

Read article →
Software Testing

Shift-Left Testing Explained: Find Bugs Earlier Without Slowing the Team

Shift-left testing explained: what it means, why early defects are cheaper, practices from requirement reviews to CI checks, the tester's…

Read article →
Artificial Intelligence

LLM Structured Output Explained: Getting Reliable JSON From AI Models

LLM structured output explained with validation we ran in Pydantic: JSON mode and schemas, constrained decoding, the ways model output…

Read article →
Test Automation

API Contract Testing Explained: Catch Breaking Changes Before They Ship

API contract testing explained with a schema check we ran: what a contract is, consumer-driven contracts, Pact, OpenAPI, which changes…

Read article →
Software Testing

Risk-Based Testing Explained: Score Risks, Build a Matrix and Test What Matters

Risk-based testing explained step by step: identifying risks, scoring likelihood and impact, building a risk matrix, mapping risk to test…

Read article →