Intermediate LevelPython
How does functools.partial() fix arguments of existing functions to create new specialized functions (Currying)?
❓ Interview Prompt: Explain `functools.partial()`, freezing positional/keyword parameters, creating specialized function signatures, and Currying.
*'functools.partial(func, args, keywords)' creates a new callable wrapper that freezes a subset of function arguments:
Use Case:
Creating specialized helper functions without writing duplicate 'def' wrapper boilerplate or lambda expressions.
Code Implementation Example
python
from functools import partial
def multiply(x, y):
return x * y
# Create specialized 'double' function freezing x=2
double = partial(multiply, 2)
print(double(5)) # Output: 10
# Parse binary strings via specialized int partial
parse_binary = partial(int, base=2)
print(parse_binary('1010')) # Output: 10💡Creating specialized functions with frozen parameters using functools.partial.
🎯 Interview Key Takeaway
Use functools.partial to freeze function parameters and create specialized reusable callables.
#partial#functools#Currying#Intermediate
Back to all Python Questions→