Explanation:
1. Understanding partial from functools
The partial function is used to fix some arguments of a function, creating a new function with fewer parameters.
2. Defining the Function add
def add(x, y, z):
return x + y + z
This function takes three parameters (x, y, and z) and returns their sum.
3. Using partial to Create add_five
add_five = partial(add, 5)
Here, partial(add, 5) creates a new function add_five where the first argument (x) is pre-filled with 5.
add_five(y, z) is now equivalent to calling add(5, y, z).
4. Calling add_five(3, 2)
print(add_five(3, 2))
Since add_five is partial(add, 5), calling add_five(3, 2) is the same as calling:
add(5, 3, 2)
5 + 3 + 2 = 10, so the output is:
Output:
10
0 Comments:
Post a Comment