Partial functions in Python
In this post, we will see about partial functions in Python.
What is a partial function?
- A partial function is a function that is derived from another function.
- These allow us to fix a certain number of argument to a function and generate new ones.
Example
from functools import partial
# Normal function
def add(a, b, c, x):
return a + b + c + x
# partial function with 3 fixed arguments
temp = partial(add, 2,3,4)
# Calling temp()
print(temp(1))
This will output
10
Check output here
We can also pass parameterized arguments.
Example
from functools import partial
# Normal function
def add(a, b, c, x):
return a + b + c + x
# partial function with 3 fixed arguments
temp = partial(add, c=2,b=3,x=4)
# Calling temp()
print(temp(1))
This will output
10