Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order in which different forms of assignments may appear in a python function call?

Tags:

python

When I run the following code, I get an error, why is that? Isn't the order of the arguments correct?

def f(a, b, c, d):
    print a, b, c, d

f(1, b=2, *(3,), **{'d': 4})

This is the error that I'm getting:

Traceback (most recent call last):
  File "/home/asad/scripts/l.py", line 9, in <module>
    f(1, b=2, *(3,), **{'d': 4})
TypeError: f() got multiple values for keyword argument 'b'
[Finished in 0.1s with exit code 1]
like image 312
asad Avatar asked Mar 19 '23 23:03

asad


1 Answers

b=2 in the function call is not a variable assignment but a passing of a keyword argument.

You are passing b as a keyword argument yet you're also passing the value 3 (as the second positional argument), which is also b.

So b is receiving multiple values in that function call.

like image 51
Simeon Visser Avatar answered Apr 29 '23 17:04

Simeon Visser