If I define a class method with a keyword argument thus:
class foo(object):
def foodo(thing=None, thong='not underwear'):
print thing if thing else "nothing"
print 'a thong is',thong
calling the method generates a TypeError
:
myfoo = foo()
myfoo.foodo(thing="something")
...
TypeError: foodo() got multiple values for keyword argument 'thing'
What's going on?
This question is related to
python
class
python-2.7
methods
Thanks for the instructive posts. I'd just like to keep a note that if you're getting "TypeError: foodo() got multiple values for keyword argument 'thing'", it may also be that you're mistakenly passing the 'self' as a parameter when calling the function (probably because you copied the line from the class declaration - it's a common error when one's in a hurry).
Also this can happen in Django if you are using jquery ajax to url that reverses to a function that doesn't contain 'request' parameter
$.ajax({
url: '{{ url_to_myfunc }}',
});
def myfunc(foo, bar):
...
I want to add one more answer :
It happens when you try to pass positional parameter with wrong position order along with keyword argument in calling function.
there is difference between parameter and argument
you can read in detail about here Arguments and Parameter in python
def hello(a,b=1, *args):
print(a, b, *args)
hello(1, 2, 3, 4,a=12)
since we have three parameters :
a is positional parameter
b=1 is keyword and default parameter
*args is variable length parameter
so we first assign a as positional parameter , means we have to provide value to positional argument in its position order, here order matter. but we are passing argument 1 at the place of a in calling function and then we are also providing value to a , treating as keyword argument. now a have two values :
one is positional value: a=1
second is keyworded value which is a=12
We have to change hello(1, 2, 3, 4,a=12)
to hello(1, 2, 3, 4,12)
so now a will get only one positional value which is 1 and b will get value 2 and rest of values will get *args (variable length parameter)
if we want that *args should get 2,3,4 and a should get 1 and b should get 12
then we can do like this
def hello(a,*args,b=1):
pass
hello(1, 2, 3, 4,b=12)
Something more :
def hello(a,*c,b=1,**kwargs):
print(b)
print(c)
print(a)
print(kwargs)
hello(1,2,1,2,8,9,c=12)
output :
1
(2, 1, 2, 8, 9)
1
{'c': 12}
This might be obvious, but it might help someone who has never seen it before. This also happens for regular functions if you mistakenly assign a parameter by position and explicitly by name.
>>> def foodo(thing=None, thong='not underwear'):
... print thing if thing else "nothing"
... print 'a thong is',thong
...
>>> foodo('something', thing='everything')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foodo() got multiple values for keyword argument 'thing'
This error can also happen if you pass a key word argument for which one of the keys is similar (has same string name) to a positional argument.
>>> class Foo():
... def bar(self, bar, **kwargs):
... print(bar)
...
>>> kwgs = {"bar":"Barred", "jokes":"Another key word argument"}
>>> myfoo = Foo()
>>> myfoo.bar("fire", **kwgs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bar() got multiple values for argument 'bar'
>>>
"fire" has been accepted into the 'bar' argument. And yet there is another 'bar' argument present in kwargs.
You would have to remove the keyword argument from the kwargs before passing it to the method.
just add 'staticmethod' decorator to function and problem is fixed
class foo(object):
@staticmethod
def foodo(thing=None, thong='not underwear'):
print thing if thing else "nothing"
print 'a thong is',thong
Source: Stackoverflow.com