问题:TypeError:参数有多个值
我读了与该错误有关的其他线程,似乎我的问题与到目前为止所读的所有帖子都有一个有趣的明显区别,即,到目前为止,所有其他帖子在创建一个用户方面都存在错误类或内置系统资源。调用函数时遇到此问题,我无法弄清楚它的用途。有任何想法吗?
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)
错误信息:
Horizontol_drawbox(BOX_LENGTH, fillBox = True)
TypeError: Horizontol_drawbox() got multiple values for argument 'fillBox'
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'
回答 0
当指定关键字参数覆盖位置参数时,会发生这种情况。例如,让我们想象一个绘制彩色框的函数。该函数选择要使用的颜色,并将框的图形委托给另一个函数,从而中继所有其他参数。
def color_box(color, *args, **kwargs):
painter.select_color(color)
painter.draw_box(*args, **kwargs)
然后打电话
color_box("blellow", color="green", height=20, width=30)
将失败,因为将两个值分配给了color
:"blellow"
作为position和"green"
as关键字。(painter.draw_box
应该接受height
和width
参数)。
在示例中很容易看到这一点,但是当然,如果在调用时混淆了参数,则调试起来可能并不容易:
# misplaced height and width
color_box(20, 30, color="green")
在这里,color
被分配20
,然后args=[30]
又color
被分配"green"
。
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"
.
回答 1
我遇到的问题确实很容易解决,但花了我一段时间才看穿。
我已经将声明复制到我使用它的地方,并且在此处留下了“自我”的论点,但是我花了很长时间才意识到这一点。
我有
self.myFunction(self, a, b, c='123')
但这应该是
self.myFunction(a, b, c='123')
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')
回答 2
如果您忘记了self
类方法中的声明,也会发生这种情况。
例:
class Example():
def is_overlapping(x1, x2, y1, y2):
# Thanks to https://stackoverflow.com/a/12888920/940592
return max(x1, y1) <= min(x2, y2)
未能通过以下方式调用它self.is_overlapping(x1=2, x2=4, y1=3, y2=5)
:
{TypeError} is_overlapping()为参数“ x1”获得了多个值
作品:
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)
This also happens if you forget self
declaration 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) <= min(x2, y2)
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) <= min(x2, y2)
回答 3
我的问题类似于Q —十的问题,但是在我的情况下,我忘记了为类函数提供self参数:
class A:
def fn(a, b, c=True):
pass
应该成为
class A:
def fn(self, a, b, c=True):
pass
当以如下方式调用类方法时,很难看到这种错误的实现:
a_obj = A()
a.fn(a_val, b_val, c=False)
这将产生一个TypeError: got multiple values for argument
。希望这里的其余答案很清楚,任何人都可以快速理解并解决错误。如果没有,希望这个答案对您有帮助!
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:
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!
回答 4
简而言之,您不能执行以下操作:
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
因为您将变量y两次传递给函数:一次作为kwargs,一次作为函数声明。
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
我被带到这里的原因是到目前为止答案中没有明确提及的原因,因此可以避免其他麻烦:
如果函数参数已更改顺序,也会发生错误-出于与接受的答案相同的原因:位置参数与关键字参数发生冲突。
就我而言,这是因为Pandas set_axis
函数的参数顺序在0.20和0.22之间变化:
0.20: DataFrame.set_axis(axis, labels)
0.22: DataFrame.set_axis(labels, axis=0, inplace=None)
使用set_axis的常见示例会导致此令人困惑的错误,因为在您调用时:
df.set_axis(['a', 'b', 'c'], axis=1)
将0.22之前的['a', 'b', 'c']
值分配给axis,因为它是第一个参数,然后位置参数提供“多个值”。
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”.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。