functools partial
The functools
module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.
functools.partial
Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords.
- If more arguments are supplied to the call, they are appended to args.
- If additional keyword arguments are supplied, they extend and override keywords.
import functools
print(functools.partial.__doc__)
partial(func, *args, **keywords) - new function with partial application
of the given arguments and keywords.
def foo(name):
print(f'Inside foo : name = {name}')
foo('Krishan')
Inside foo : name = Krishan
Partial with default positional argument
foo_with_default = functools.partial(foo, 'Krishan')
foo_with_default()
Inside foo : name = Krishan
If name
is passed again as keyword argument or positional arg, the decorated function will throw error.
foo_with_default(name = 'Ram')
---------------------------------------------------------------------------
TypeError: foo() got multiple values for argument 'name'
foo_with_default('Ram')
---------------------------------------------------------------------------
TypeError: foo() takes 1 positional argument but 2 were given
Partial with default Keyword argument
foo_with_default = functools.partial(foo, name = 'Krishan')
If name
is passed again as a keyword argument, the kwargs gets updated before calling foo
(the function being decorated.).
foo_with_default(name = 'Ram')
Inside foo : name = Ram
Same is not true for passing the argument as positional argument as positional arguments get appended which results in error.
foo_with_default('Ram')
---------------------------------------------------------------------------
TypeError: foo() takes 1 positional argument but 2 were given