chopper draw lion4
chopper draw lion4

Reputation: 13487

TypeError: got multiple values for argument

I have this code:

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)

But I get an error that says:

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

What does this mean, and how can I fix the problem?

Upvotes: 373

Views: 716586

Answers (9)

Akaisteph7
Akaisteph7

Reputation: 6416

In my case, I wasn't explicitly passing the same argument twice. But the django_rq library function I was calling (queue.enqueue) was doing so internally. It seems it would automatically pass a value for any explicitly defined argument whether or not it was positional and THEN also add on any extra positional arguments.

So, I had to change my function definition to:

...
    ...
    queue = django_rq.get_queue('queue_name')
    job = queue.enqueue(
        function_to_call,
        arguments,
        keyword_arg=keyword_arg,
    )

...

# def function_to_call(arguments, keyword_arg=None): # OLD
def function_to_call(arguments, **kwargs): # NEW

Upvotes: 0

I was getting the same error in the following code:

re_path(r'^(?P<pos_id>\d+)/ctbl/(?P<ctbl_id>\d+)/set_commissioni/$', pos_forms.set_commissioni, name=url_views_pos_ctbl)

and the function:

def set_commissioni(pos_id=None, ctbl_id=None):

the error raised because i forgot request as first parameter of function

It would be:

def set_commissioni(request, pos_id=None, ctbl_id=None):

Upvotes: 2

Q---ten
Q---ten

Reputation: 2526

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.my_method(self, a, b, c='123')

but it should have been

self.my_method(a, b, c='123')

Upvotes: 197

Alist
Alist

Reputation: 139

I was getting the same error as I was using @classmethod.

If someone is getting this error while using @classmethod in the class, don't forget to pass the cls argument into the method:

class X:
  @classmethod
  def my_method(x):
    pass

The above code will raise cause the "TypeError: got multiple values for argument" error and you should pass cls like folloing:

class X:
  @classmethod
  def my_method(cls, x):
    pass

Upvotes: 2

Phoenix
Phoenix

Reputation: 4274

This exception also will be raised whenever a function has been called with the combination of keyword arguments and args, kwargs

Example:

def function(a, b, c, *args, **kwargs):
    print(f"a: {a}, b: {b}, c: {c}, args: {args}, kwargs: {kwargs}")

function(a=1, b=2, c=3, *(4,))

And it'll raise:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-1dcb84605fe5> in <module>
----> 1 function(a=1, b=2, c=3, *(4,))

TypeError: function() got multiple values for argument 'a'

And Also it'll become more complicated, whenever you misuse it in the inheritance. so be careful we this stuff!

1- Calling a function with keyword arguments and args:

class A:
    def __init__(self, a, b, *args, **kwargs):
        self.a = a
        self.b = b
    
class B(A):
    def __init__(self, *args, **kwargs):

        a = 1
        b = 2
        super(B, self).__init__(a=a, b=b, *args, **kwargs)

B(3, c=2)

Exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-17e0c66a5a95> in <module>
     11         super(B, self).__init__(a=a, b=b, *args, **kwargs)
     12 
---> 13 B(3, c=2)

<ipython-input-5-17e0c66a5a95> in __init__(self, *args, **kwargs)
      9         a = 1
     10         b = 2
---> 11         super(B, self).__init__(a=a, b=b, *args, **kwargs)
     12 
     13 B(3, c=2)

TypeError: __init__() got multiple values for argument 'a'

2- Calling a function with keyword arguments and kwargs which it contains keyword arguments too:

class A:
    def __init__(self, a, b, *args, **kwargs):
        self.a = a
        self.b = b
    
class B(A):
    def __init__(self, *args, **kwargs):

        a = 1
        b = 2
        super(B, self).__init__(a=a, b=b, *args, **kwargs)

B(**{'a': 2})

Exception:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-c465f5581810> in <module>
     11         super(B, self).__init__(a=a, b=b, *args, **kwargs)
     12 
---> 13 B(**{'a': 2})

<ipython-input-7-c465f5581810> in __init__(self, *args, **kwargs)
      9         a = 1
     10         b = 2
---> 11         super(B, self).__init__(a=a, b=b, *args, **kwargs)
     12 
     13 B(**{'a': 2})

TypeError: __init__() got multiple values for keyword argument 'a'

Upvotes: 10

Heath Raftery
Heath Raftery

Reputation: 4149

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

Upvotes: 8

gies0r
gies0r

Reputation: 5239

This also happens if you forget selfdeclaration 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)

Upvotes: 121

quasipolynomial
quasipolynomial

Reputation: 736

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.

Upvotes: 6

Cilyan
Cilyan

Reputation: 8471

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

Upvotes: 509

Related Questions