Code Explanation:
from functools import partial
partial is a function from Python’s functools module.
It lets you pre-fill (or "fix") some arguments of a function, creating a new function with fewer parameters.
2. Define a General Power Function
def power(base, exp):
return base ** exp
This is a regular function that takes two arguments: base and exp (exponent).
It returns base raised to the power of exp.
3. Create a square Function Using partial
square = partial(power, exp=2)
This creates a new function square by fixing exp=2 in the power function.
Now square(x) behaves like power(x, 2), i.e., it returns the square of x.
It is equivalent to writing:
def square(x):
return power(x, 2)
4. Call the square Function
print(square(4))
This evaluates power(4, 2) → 4 ** 2 → 16
So, it prints:
16
Final Output
16
.png)

0 Comments:
Post a Comment