TypeError: got multiple values for argument in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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.

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'}

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}

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')

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.

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

main.py
class Employee(): # ✅ specified self as 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.

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 instance methods (methods that you call on a class instance).

If you have a basic function, you shouldn't specify a self argument.

Conclusion #

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.

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.