Last updated: Apr 8, 2024
Reading timeยท3 min

The Python "TypeError: got multiple values for argument" occurs when we overwrite the value of a positional argument with a keyword argument in a function call.
To solve the error, make sure to only pass a value for the argument once and
specify self as the first arg in class methods.

Here is an example of how the error occurs.
def get_employee(name, **kwargs): return {'name': name, **kwargs} # โ๏ธ TypeError: get_employee() got multiple values for argument 'name' result = get_employee('Alice', name='Alice') print(result)
The get_employee function takes a name positional argument and keyword
arguments.
name positional argument and used a keyword argument to set a value for name as well.Specifying multiple values for the same argument is not allowed.
One way to solve the error is to remove the name keyword argument from the
function call.
def get_employee(name, **kwargs): return {'name': name, **kwargs} result = get_employee('Alice') print(result) # ๐๏ธ {'name': 'Alice'}

We only called the function with a positional argument for the name parameter.
Alternatively, you can pass the name argument as a keyword argument only.
def get_employee(name, **kwargs): return {'name': name, **kwargs} result = get_employee(name='Alice', salary=100) print(result) # ๐๏ธ {'name': 'Alice', 'salary': 100}

We called the get_employee function with 2 keyword arguments, so Python is no
longer confused about values for which parameters are provided.
self as the first argument in a classAnother common cause of the error is forgetting to specify self as the first
argument in a class method.
class Employee(): # ๐๏ธ forgot to specify `self` as first arg def get_name(name=None): return name emp1 = Employee() # โ๏ธ TypeError: Employee.get_name() got multiple values for argument 'name' emp1.get_name(name='Alice')

We didn't specify self as the first
argument in the get_name method which caused the error.
self as the first argument to the method.self represents an instance of the class, so when we assign a variable as
self.my_var = 'some value', we are declaring an instance variable - a variable
unique to each instance.
You could name this argument anything because the name self has no special
meaning in Python.
self as the first argument in the methodTo solve the error, make sure to specify self as the first argument in the
method.
class Employee(): # โ Specified self as the first arg def get_name(self, name=None): return name emp1 = Employee() print(emp1.get_name(name='Alice')) # ๐๏ธ "Alice"

Now that we set self as the first argument in the method, everything works as
expected.
The issue was caused because Python automatically passes the instance (self)
as the first argument to the method.
So, if you don't explicitly specify the self arg in the definition of the
method, Python passes self and we pass name='Alice' as a value for the same
argument.
class Employee(): # ๐๏ธ forgot to specify `self` as first arg def get_name(name=None): return name emp1 = Employee() # โ๏ธ TypeError: Employee.get_name() got multiple values for argument 'name' print(emp1.get_name(name='Alice'))
emp1.get_name(), it will get passed the instance as the first argument, so you should make sure to explicitly specify it when declaring the method.Otherwise, you and Python might end up passing a value for the same argument, which is very confusing.
Note that you should only specify self as the first argument in the instance
methods (methods that you call on a class instance).
If you have a basic function, you shouldn't specify a self argument.