functools.partial时的一个报错
def sum(x,y) :
return x+y
f = functools.partial( f, y=1 )
f(2)
3 # Works as expected
def sum(x,y) :
return x+y
f = functools.partial( f, x=1 )
f(2)
TypeError: sum() got multiple values for argument 'x'
报错原因:
Python first fulfils the positional arguments, and your first argument is x
. Then the keyword arguments are applied, and you again supplied x
.
functools.partial()
has no means to detect that you already supplied the first positional argument as a keyword argument instead. It will not augment your call by replacing the positional argument with a y=
keyword argument.
When mixing positional and keyword arguments, you must take care not to use the same argument twice.
ref: functools.partial wants to use a positional argument as a keyword argument