all Python 3functoolsterm
functools.partial
partial
Creates a new function with some of the arguments already filled in (partial application).
Syntax
from functools import partial
functools.partial(func, /, *args, **kwargs) -> partial
Arguments are frozen when the partial is created, not when it is called — that is the difference from a lambda, whose late binding makes every closure in a loop see the variable's final value. Positional arguments are glued on from the left, so to skip the first parameter and pre-fill only the second you use a keyword or, since 3.14, the functools.Placeholder sentinel. Unlike a lambda, a partial object exposes func, args and keywords, prints readably while debugging, and can be pickled.
Examples
from functools import partial
def power(base, exp):
return base ** exp
square = partial(power, exp=2)
print(square(4)) # → 16
cube = partial(power, exp=3)
print(cube(3)) # → 27
add = lambda a, b: a + b
add10 = partial(add, 10)
print(add10(5)) # → 15
print_sep = partial(print, sep=', ')
print_sep(1, 2, 3) # → 1, 2, 3
# partial с методом
from functools import partial
int2 = partial(int, base=2)
print(int2('1010')) # → 10
See also
Open in the full glossarydocs.python.orgReport an inaccuracy