Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: got multiple values for argument when passing in *args [duplicate]

Everytime I try to assign a default argument a value, when also using *args, it raises the error: TypeError: "got multiple values for argument".

def test(a,b, c=3, *args):
    pass

args = [1,2,3]
test(1,2,c=3, *args)

.

TypeError: test() got multiple values for argument 'c'

At first I thought I found the answer here: TypeError: got multiple values for argument But it turns out that the first argument in that qusetion was being explictly overwritten, so in my own example this would be the same thing as saying test(1,2, a=3, *args) where a would be given values 1 and 3. However here that is not the case.

This question might be a possible duplicate of this one "got multiple values for keyword argument" when using *args, **kwargs in a python function but honestly, there are a lot more details in that question that is beyond the scope of what am asking for and I really can't follow it through. So if this is a duplicate, then it abstracts everything away from the other question down to the actual problem itself.

like image 305
Jim Jam Avatar asked Jan 27 '26 01:01

Jim Jam


1 Answers

You need to swap it so the c=3 is last because python requires optional arguments to be at the end. The code functions when it is done like this:

def test(a,b, *args, c=3):
    pass

args = [1,2,3]
test(1,2, *args, c=3)
like image 71
Hippolippo Avatar answered Jan 29 '26 13:01

Hippolippo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!