TypeError: got multiple values for argument

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

I read the other threads that had to do with this error and it seems that my problem has an interesting distinct difference than all the posts I read so far, namely, all the other posts so far have the error in regards to either a user created class or a builtin system resource. I am experiencing this problem when calling a function, I can't figure out what it could be for. Any ideas?

BOX_LENGTH = 100 turtle.speed(0) fill = 0 for i in range(8):     fill += 1     if fill % 2 == 0:         Horizontol_drawbox(BOX_LENGTH, fillBox = False)     else:         Horizontol_drawbox(BOX_LENGTH, fillBox = True)      for i in range(8):         fill += 1         if fill % 2 == 0:             Vertical_drawbox(BOX_LENGTH,fillBox = False)         else:             Vertical_drawbox(BOX_LENGTH,fillBox = True) 

Error message:

    Horizontol_drawbox(BOX_LENGTH, fillBox = True) TypeError: Horizontol_drawbox() got multiple values for argument 'fillBox' 

回答1:

This happens when a keyword argument is specified that overwrites a positional argument. For example, let's imagine a function that draws a colored box. The function selects the color to be used and delegates the drawing of the box to another function, relaying all extra arguments.

def color_box(color, *args, **kwargs):     painter.select_color(color)     painter.draw_box(*args, **kwargs) 

Then the call

color_box("blellow", color="green", height=20, width=30) 

will fail because two values are assigned to color: "blellow" as positional and "green" as keyword. (painter.draw_box is supposed to accept the height and width arguments).

This is easy to see in the example, but of course if one mixes up the arguments at call, it may not be easy to debug:

# misplaced height and width color_box(20, 30, color="green") 

Here, color is assigned 20, then args=[30] and color is again assigned "green".



回答2:

I had the same problem that is really easy to make, but took me a while to see through.

I had copied the declaration to where I was using it and had left the 'self' argument there, but it took me ages to realise that.

I had

self.myFunction(self, a, b, c='123') 

but it should have been

self.myFunction(a, b, c='123') 


回答3:

This also happens if you forget selfdeclaration inside class methods.

Example:

class Example():     def is_overlapping(x1, x2, y1, y2):         # Thanks to https://stackoverflow.com/a/12888920/940592         return max(x1, y1) 

Fails calling it like self.is_overlapping(x1=2, x2=4, y1=3, y2=5) with:

{TypeError} is_overlapping() got multiple values for argument 'x1'

WORKS:

class Example():     def is_overlapping(self, x1, x2, y1, y2):         # Thanks to https://stackoverflow.com/a/12888920/940592         return max(x1, y1) 


回答4:

Simply put you can't do the following:

class C(object):     def x(self, y, **kwargs):         # Which y to use, kwargs or declaration?          pass  c = C() y = "Arbitrary value" kwargs["y"] = "Arbitrary value" c.x(y, **kwargs) # FAILS 

Because you pass the variable 'y' into the function twice: once as kwargs and once as function declaration.



回答5:

I was brought here for a reason not explicitly mentioned in the answers so far, so to save others the trouble:

The error also occurs if the function arguments have changed order - for the same reason as in the accepted answer: the positional arguments clash with the keyword arguments.

In my case it was because the argument order of the Pandas set_axis function changed between 0.20 and 0.22:

0.20: DataFrame.set_axis(axis, labels) 0.22: DataFrame.set_axis(labels, axis=0, inplace=None) 

Using the commonly found examples for set_axis results in this confusing error, since when you call:

df.set_axis(['a', 'b', 'c'], axis=1) 

prior to 0.22, ['a', 'b', 'c'] is assigned to axis because it's the first argument, and then the positional argument provides "multiple values".



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!