Python 类型错误:有多个参数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21764770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:33:47  来源:igfitidea点击:

TypeError: got multiple values for argument

pythonpython-3.x

提问by chopper draw lion4

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'

采纳答案by Cilyan

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_boxis supposed to accept the heightand widtharguments).

将失败,因为两个值被分配给color"blellow"作为位置和"green"作为关键字。(painter.draw_box应该接受heightwidth论点)。

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, coloris assigned 20, then args=[30]and coloris again assigned "green".

这里,color被赋值20,然后args=[30]color被赋值"green"

回答by Q---ten

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')

回答by quasipolynomial

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.

因为您将变量 'y' 传递给函数两次:一次作为 kwargs,一次作为函数声明。

回答by gies0r

This also happens if you forget selfdeclaration inside class methods.

如果您忘记self在类方法中声明,也会发生这种情况。

Example:

例子:

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

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

无法像这样调用它self.is_overlapping(x1=2, x2=4, y1=3, y2=5)

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

{TypeError} is_overlapping() 为参数“x1”获得了多个值

WORKS:

作品

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

回答by Heath Raftery

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_axisfunction changed between 0.20 and 0.22:

就我而言,这是因为 Pandasset_axis函数的参数顺序在 0.20 和 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:

使用 set_axis 的常见示例会导致此令人困惑的错误,因为当您调用时:

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".

在 0.22 之前,['a', 'b', 'c']分配给轴,因为它是第一个参数,然后位置参数提供“多个值”。

回答by Andreas Forsl?w

My issue was similar to Q---ten's, but in my case it was that I had forgotten to provide the self argument to a class function:

我的问题与 Q---ten 类似,但在我的情况下,我忘记为类函数提供 self 参数:

class A:
    def fn(a, b, c=True):
        pass

Should become

应该成为

class A:
    def fn(self, a, b, c=True):
        pass

This faulty implementation is hard to see when calling the class method as:

当调用类方法时很难看到这个错误的实现:

a_obj = A()
a.fn(a_val, b_val, c=False)

Which will yield a TypeError: got multiple values for argument. Hopefully, the rest of the answers here are clear enough for anyone to be able to quickly understand and fix the error. If not, hope this answer helps you!

这将产生一个TypeError: got multiple values for argument. 希望这里的其余答案足够清楚,让任何人都能够快速理解并修复错误。如果没有,希望这个答案对你有帮助!