Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: got multiple values for argument" after applying functools.partial() [duplicate]

Tags:

python

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.

like image 687
s3rvac Avatar asked Oct 03 '14 15:10

s3rvac


1 Answers

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

like image 86
Marek Avatar answered Sep 27 '22 23:09

Marek