functools

Stdlib — FunctionalPython 2.5+Intermediate

Higher-order functions: lru_cache, partial, reduce, wraps, singledispatch

Quick Info

Documentation
Official Docs
Python Version
2.5+
Dependencies
None — Python Standard Library
Install
Included with Python

Learn by Difficulty

Quick Example

python
from functools import reduce, lru_cache

# reduce
total = reduce(lambda a, b: a + b, [1, 2, 3, 4, 5])
print(f"Sum: {total}")

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

print(f"fib(10): {fib(10)}")
print(f"fib(30): {fib(30)}")

The functools module is part of Python's standard library. Higher-order functions: lru_cache, partial, reduce, wraps, singledispatch.

Try in Playground

Tags

stdlibfunctional-programmingdecoratorcaching

Related Items