"TypeError: got multiple values ​​for argument" after using functools.partial ()

Consider the following piece of code that uses functools.partial()

:

import functools

def add(a, b):
    return a + b

add_10 = functools.partial(add, a=10)
add_10(4)

      

When I ran 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 on the penultimate line, it passes:

add_10 = functools.partial(add, 10)

      

Why doesn't it pass in the first case? I am using Python 3.4.

+3


source to share


1 answer


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 a default value must be at the end. So b = 10 instead of a = 10

+5


source







All Articles