TypeError: got multiple values for argument in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# TypeError: got multiple values for argument in Python

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.

typeerror got multiple values for argument

Here is an example of how the error occurs.

main.py
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.

When calling the function, we passed a value for the 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.

# Removing the keyword argument from the function call

One way to solve the error is to remove the name keyword argument from the function call.

main.py
def get_employee(name, **kwargs): return {'name': name, **kwargs} result = get_employee('Alice') print(result) # ๐Ÿ‘‰๏ธ {'name': 'Alice'}

remove keyword argument from function call

We only called the function with a positional argument for the name parameter.

# Passing the value as a keyword argument only

Alternatively, you can pass the name argument as a keyword argument only.

main.py
def get_employee(name, **kwargs): return {'name': name, **kwargs} result = get_employee(name='Alice', salary=100) print(result) # ๐Ÿ‘‰๏ธ {'name': 'Alice', 'salary': 100}

pass value as keyword argument only

We called the get_employee function with 2 keyword arguments, so Python is no longer confused about values for which parameters are provided.

# Forgetting to specify self as the first argument in a class

Another common cause of the error is forgetting to specify self as the first argument in a class method.

main.py
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')

forgetting to specify self as first argument

We didn't specify self as the first argument in the get_name method which caused the error.

When we call an instance method, Python automatically passes 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.

# Specify self as the first argument in the method

To solve the error, make sure to specify self as the first argument in the method.

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

specify self as first argument in method

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.

main.py
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'))
Every time you call an instance method, e.g. 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev