Python functools Module
Example
Use a cached function:
from functools import lru_cache
@lru_cache(maxsize=2)
def add(a, b):
return a + b
print(add(1, 2))
print(hasattr(add, "cache_info"))
Try it Yourself »
Definition and Usage
The functools module provides higher-order functions and operations on callable objects.
Use it for caching, partial function application, decorators, and tools that help build function-based utilities.
Members
Member | Description |
---|---|
cache() | Simple unbounded cache decorator (like lru_cache with no size limit). |
cached_property | Descriptor that caches the result of a method as a property. |
cmp_to_key() | Convert an old-style comparison function to a key function. |
lru_cache() | Decorator to wrap a function with a Least-Recently-Used cache. |
partial | Create a new function with partial application of the given arguments. |
partialmethod | Partial version of a method for use in classes. |
reduce() | Apply a function of two arguments cumulatively to the items of a sequence. |
singledispatch() | Decorator for single-dispatch generic functions. |
singledispatchmethod() | Single-dispatch generic method descriptor. |
total_ordering() | Class decorator that fills in missing ordering methods. |
update_wrapper() | Update a wrapper function to look like the wrapped function. |
wraps() | Decorator to update a wrapper function with attributes of the wrapped function. |