Partial Functions
Use functools.partial.
What is a partial?
functools.partial creates a new function with some arguments of an existing function already filled in. It is like pre-loading arguments so you can call the result with fewer.
Import it with from functools import partial.
from functools import partial
def power(base, exp):
return base ** exp
square = partial(power, exp=2)
print(square(5))Fixing positional arguments
Positional arguments passed to partial fill the function's parameters from the left. Remaining arguments are supplied when you call the partial.
from functools import partial
def multiply(a, b):
return a * b
double = partial(multiply, 2)
print(double(10))
print(double(50))