Explanation:
๐น Step 1: Import partial
from functools import partial
partial() is a utility from the functools module.
It allows you to:
Fix some arguments of a function
in advance.
Think of it as creating a new function with some arguments already filled in.
๐น Step 2: Create Partial Function
f = partial(pow, 2)
Original function:
pow(a, b)
Meaning:
a ** b
Examples:
pow(2,3) → 8
pow(3,2) → 9
Now:
partial(pow, 2)
fixes the first argument as:
2
So Python creates a new function equivalent to:
def f(b):
return pow(2, b)
๐น Step 3: Execute f(5)
f(5)
Internally becomes:
pow(2, 5)
Because:
2
was already fixed by partial().
๐น Step 4: Calculate Power
pow(2, 5)
means:
2 × 2 × 2 × 2 × 2
Result:
32
๐น Step 5: Print Result
print(32)
Output:
32

0 Comments:
Post a Comment