TypeError: got multiple values for argument

前端 未结 7 2356
忘掉有多难
忘掉有多难 2020-11-28 06:13

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

7条回答
  •  情书的邮戳
    2020-11-28 06:30

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

提交回复
热议问题