How to Solve "TypeError: got multiple values for argument" in Python
The TypeError: got multiple values for argument
error in Python occurs when you accidentally provide the same argument to a function or method more than once. This usually happens due to a mix-up between positional and keyword arguments, or when forgetting the self
parameter in class methods.
This guide explains the causes and provides clear solutions.
Understanding Positional and Keyword Arguments
Before diving into the error, it's important to understand the two main ways to pass arguments to functions in Python:
-
Positional Arguments: These are passed based on their position in the function call. The order matters.
def greet(name, greeting):
print(f"{greeting}, {name}!")
greet("Alice", "Hello") # Correct: name="Alice", greeting="Hello" -
Keyword Arguments: These are passed using the name of the parameter, like
name=value
. The order doesn't matter when you use keyword arguments.greet(greeting="Hi", name="Bob") # Correct: name="Bob", greeting="Hi"
Common Causes and Solutions
Mixing Positional and Keyword Arguments Incorrectly
The most frequent cause of this error is providing a value for the same parameter both positionally and as a keyword argument:
def get_employee(name, **kwargs):
return {'name': name, **kwargs}
# ⛔️ TypeError: get_employee() got multiple values for argument 'name'
# result = get_employee('Alice', name='Alice') # Incorrect: 'name' provided twice
- The example defines a function that takes a parameter called
name
, and then attempts to call it, passing a positional argument'Alice'
, and also a keyword argumentname='Alice'
. This means we are passing a value for thename
twice. - You've given a value for
name
twice: once as the first positional argument ('Alice'
) and again as a keyword argument (name='Alice'
).
Solutions:
-
Use either positional or keyword arguments for the same parameter, not both:
result = get_employee('Alice', salary=100) # Correct: positional for name, keyword for others
print(result) # Output: {'name': 'Alice', 'salary': 100}
result = get_employee(name='Alice', salary=100) # Correct: all keyword arguments
print(result) # Output: {'name': 'Alice', 'salary': 100} -
If using
**kwargs
: If your function accepts arbitrary keyword arguments via**kwargs
, make sure you're not also using a positional argument with the same name:def get_employee(name, **kwargs): # Correct use of **kwargs
return {'name': name, **kwargs}
result = get_employee('Alice', salary=100) # Correct
print(result) # Output: {'name': 'Alice', 'salary': 100}
Forgetting self
in Instance Methods
Another very common cause, especially for beginners, is forgetting the self
parameter in class instance methods:
class Employee():
# def get_name(name=None): # ⛔️ INCORRECT: Missing self
# return name
def get_name(self, name=None): #Correct
return name
emp1 = Employee()
# ⛔️ TypeError: Employee.get_name() got multiple values for argument 'name'
# emp1.get_name(name='Alice')
#Correct way:
print(emp1.get_name(name='Alice')) # Output: Alice
-
When you call an instance method (like
emp1.get_name(...)
), Python automatically passes the instance (emp1
) as the first argument. Your method definition must includeself
to receive this argument. -
Solution: Always include
self
as the first parameter in instance method definitions:class Employee():
def get_name(self, name=None): # Correct: self is the first parameter
return name- Instance methods should always take
self
as a first argument.
- Instance methods should always take
Conclusion
The TypeError: got multiple values for argument
error is a signal that you've provided a value for the same function parameter more than once.
- This usually happens because of a mix-up between positional and keyword arguments or a missing
self
parameter in a class method. - Carefully review your function calls and method definitions, ensuring that each parameter receives its value only once.
- Using either positional or keyword arguments consistently helps prevent this error. Using
self
as the first parameter of instance methods is essential.