Consider the following piece of code that utilizes functools.partial()
:
import functools
def add(a, b):
return a + b
add_10 = functools.partial(add, a=10)
add_10(4)
When I run it, I got the following error:
Traceback (most recent call last):
File "test.py", line 7, in <module>
add_10(4)
TypeError: add() got multiple values for argument 'a'
When I change the keyword argument to a positional argument in the penultimate line, it passes:
add_10 = functools.partial(add, 10)
Why doesn't it pass in the first case? I'm using Python 3.4.
import functools
def add(a, b):
return a + b
add_10 = functools.partial(add, b=10)
add_10(4)
This code will work. Function arguments with default value have to be at the end. So b=10 instead of a=10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With