Partial Functions
def multiply(a, b):
return a*b
def double(a):
return multiply(a, 2)
result = double(10)
print(result) Using partials from the python in-built functools library
# import partials
# example 1
from functools import partial
def multiply(a, b):
return a * b
number = 2
partial_function = partial(multiply, number) # creates a partial
result = partial_function(10)
print(result)
# example 2
from functools import partial
def foo(x, y, z):
return x + y + z
foo_with_y_fixed = partial(foo, 5)
print(foo_with_y_fixed(1, 2)) # prints 8
print(foo_with_y_fixed(3, 4)) # prints 12Last updated