functools: partial and reduce
Create specialized functions with partial and fold sequences with reduce.
functools Overview
functools is a standard-library module of higher-order functions that operate on or return other functions.
import functools
# partial, reduce, lru_cache, wraps, cached_property ...
print(dir(functools))partial()
functools.partial(func, *args, **kw) creates a new callable with some arguments pre-filled.
import functools
def power(base, exp):
return base ** exp
square = functools.partial(power, exp=2)
cube = functools.partial(power, exp=3)
print(square(4)) # 16
print(cube(3)) # 27All lessons in this course
- itertools: Infinite and Finite Iterators
- itertools: Combinatorics
- functools: partial and reduce
- functools: lru_cache and cached_property