By ATS Staff - February 18th, 2026
Latest Technologies Python Programming Software Development
In Python, writing clean, reusable, and efficient code is a priority. The built-in functools module provides powerful tools that help you work with higher-order functions—functions that act on or return other functions. It enhances functional programming patterns and improves readability, performance, and maintainability.
This article explores the most important features of the functools module and how to use them effectively.
functools?The functools module is part of Python’s standard library. It provides higher-order functions that:
To use it:
import functools
functools.partialpartial() allows you to fix (or “pre-fill”) some portion of a function’s arguments and generate a new function.
from functools import partialdef multiply(x, y):
return x * ydouble = partial(multiply, 2)print(double(5)) # Output: 10
Here, double() always multiplies a number by 2.
functools.reducereduce() applies a function cumulatively to items of an iterable, reducing it to a single value.
from functools import reducenumbers = [1, 2, 3, 4]result = reduce(lambda x, y: x + y, numbers)print(result) # Output: 10
It computes:
(((1 + 2) + 3) + 4)
Note: Often replaced by built-in functions like
sum()when possible.
functools.lru_cachelru_cache() is one of the most powerful decorators in functools. It caches results of function calls to improve performance.
from functools import lru_cache@lru_cache(maxsize=128)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)print(fibonacci(40))
Without caching, Fibonacci recursion is slow. With lru_cache, repeated calls reuse stored results.
functools.wrapsWhen creating decorators, metadata like function name and docstring can be lost. wraps() preserves them.
from functools import wrapsdef my_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("Before function call")
return func(*args, **kwargs)
return wrapper@my_decorator
def greet():
"""Greets the user"""
print("Hello!")print(greet.__name__) # greet (not wrapper)
Without wraps, debugging and documentation tools may show incorrect function details.
functools.cmp_to_keyConverts old-style comparison functions into key functions for sorting.
from functools import cmp_to_keydef compare(a, b):
return a - bnumbers = [4, 1, 3, 2]
numbers.sort(key=cmp_to_key(compare))print(numbers)
functools.cached_propertyAvailable in Python 3.8+, cached_property works like a property but caches the result after the first call.
from functools import cached_propertyclass Data:
@cached_property
def expensive_calculation(self):
print("Computing...")
return sum(range(1000000))obj = Data()
print(obj.expensive_calculation)
print(obj.expensive_calculation) # Cached, no recomputation
functools is ImportantUsing functools helps you:
In modern frameworks like Django, FastAPI, and Flask, functools is frequently used behind the scenes for decorators, caching, and middleware logic.
The functools module is a powerful yet often underutilized part of Python’s standard library. Whether you're building APIs, optimizing algorithms, or writing clean decorators, mastering functools can significantly improve your code quality and performance.
If you're developing larger systems or frameworks, especially in backend projects, understanding functools is essential for writing efficient and maintainable Python code.