chopper draw lion4 chopper draw lion4 - 10 months ago 96
Python Question

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?

fill = 0
for i in range(8):
fill += 1
if fill % 2 == 0:
Horizontol_drawbox(BOX_LENGTH, fillBox = False)
Horizontol_drawbox(BOX_LENGTH, fillBox = True)

for i in range(8):
fill += 1
if fill % 2 == 0:
Vertical_drawbox(BOX_LENGTH,fillBox = False)
Vertical_drawbox(BOX_LENGTH,fillBox = True)

Error message:

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

Answer Source

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