Borislav Hadzhiev
Wed Apr 20 2022·3 min read
Photo by Mohamad Babayan
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'}
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}
Another 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.
To solve the error, make sure to specify self
as the first argument in the
method.
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.
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 instance
methods (methods that you call on a class instance).
If you have a basic function, you shouldn't specify a self
argument.