类型错误:有多个参数值 [英] TypeError: got multiple values for argument
问题描述
我阅读了与此错误有关的其他线程,似乎我的问题与我迄今为止阅读的所有帖子有一个有趣的明显区别,即到目前为止所有其他帖子都存在与此相关的错误用户创建的类或内置系统资源.我在调用函数时遇到了这个问题,我不知道它的用途是什么.有什么想法吗?
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)
错误信息:
Horizontol_drawbox(BOX_LENGTH, fillBox = True)
TypeError: Horizontol_drawbox() got multiple values for argument 'fillBox'
推荐答案
当指定的关键字参数覆盖位置参数时会发生这种情况.例如,让我们想象一个绘制彩色框的函数.该函数选择要使用的颜色并将框的绘制委托给另一个函数,传递所有额外的参数.
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)
然后打电话
color_box("blellow", color="green", height=20, width=30)
将失败,因为两个值被分配给 color
:"bellow"
作为位置和 "green"
作为关键字.(painter.draw_box
应该接受 height
和 width
参数).
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")
这里,color
被赋值为 20
,然后 args=[30]
和 color
被再次赋值 <代码>绿色".
Here, color
is assigned 20
, then args=[30]
and color
is again assigned "green"
.
这篇关于类型错误:有多个参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!