Skip to main content

How to Resolve Python "TypeError: takes X positional arguments but Y were given"

The TypeError: takes X positional arguments but Y were given is a common error in Python, indicating a mismatch between the number of arguments a function or method expects and the number actually provided during a call.

This guide explains the typical causes of this error (often related to the self parameter in methods or incorrect function calls) and provides clear solutions.

Understanding the Error: Argument Mismatch

This TypeError signals that you're calling a function or method with a different number of positional arguments than it's defined to accept. Python counts the arguments you provide and compares it to the function/method signature.

Cause 1: Missing self in Instance Methods

This is the most frequent cause when working with classes. Instance methods (regular methods within a class) always receive the instance itself (self) as their first argument automatically when called on an instance. If you forget to include self in the method definition, Python still passes it, leading to an argument count mismatch.

Example (Error):

class Employee():
# Incorrect: Missing 'self'
def increase_salary(a, b): # Expects 2 args (a, b)
return a + b

emp = Employee()
# Python implicitly passes 'emp' as the first argument (self)
# So, it tries to call increase_salary(emp, 100, 100) -> 3 arguments
# result = emp.increase_salary(100, 100)
# ⛔️ TypeError: Employee.increase_salary() takes 2 positional arguments but 3 were given

Solution: Add the self Parameter

Add self as the first parameter in the instance method definition:

class Employee():
# Correct: Added 'self'
def increase_salary(self, a, b): # Now expects 3 args (self, a, b)
return a + b

emp = Employee()
result = emp.increase_salary(100, 100) # Python passes 'emp' for self
print(result) # Output: 200

Alternative: Use @staticmethod

If your method doesn't need access to the instance (self), declare it as a static method using the @staticmethod decorator. Static methods don't receive self automatically.

class Employee():
@staticmethod
def increase_salary(a, b): # No 'self' needed
return a + b

emp = Employee()
result = emp.increase_salary(100, 100) # Called on instance
print(result) # Output: 200

result_class = Employee.increase_salary(50, 50) # Called on class
print(result_class) # Output: 100

Cause 2: Incorrect Number of Arguments in Function Calls

The error also occurs with regular functions if you pass more or fewer arguments than defined.

Example (Error):

def do_math(a, b): # Expects 2 arguments
return a + b

# result = do_math(10, 20, 30) # ⛔️ TypeError: do_math() takes 2 positional arguments but 3 were given

def greet(name): # Expects 1 argument
return f"Hello {name}"

# message = greet("Alice", "Smith") # ⛔️ TypeError: greet() takes 1 positional argument but 2 were given

Solution: Adjust Function Definition or Call

  • Modify the call: Ensure you pass the correct number of arguments when calling the function.

    result = do_math(10, 20)        # Correct call
    print(result) # Output: 30
  • Modify the definition: If the function should accept more/fewer arguments, update its parameter list.

    def do_math(a, b, c):           # Updated to accept 3 arguments
    return a + b + c
    result = do_math(10, 20, 30) # Now this call is valid
    print(result) # Output: 60

Cause 3: Overriding Built-in Functions (Less Common)

Accidentally defining a variable or function with the same name as a Python built-in function (like list, str, sum) can shadow the built-in and lead to this TypeError if you later try to use the built-in with the expected number of arguments.

Example (Error):

# Accidentally overriding the built-in list()
list = [1, 2, 3]

# Later... trying to use the built-in list() constructor
# my_tuple = (4, 5)
# new_list = list(my_tuple) # ⛔️ TypeError: 'list' object is not callable
# (Or a different TypeError if list was assigned another non-callable)

Solution: Avoid using names of built-in functions or types for your own variables or functions. Choose descriptive, unique names.

Specific Error Examples

  • TypeError: takes 2 positional arguments but 3 were given: Often caused by forgetting self in a method that takes two other arguments.
  • TypeError: takes 1 positional argument but 2 were given: Often caused by forgetting self in a method that takes one other argument.

The underlying cause and solutions are the same as described above.

Conclusion

The TypeError: takes X positional arguments but Y were given is usually straightforward to fix. Most often, it stems from forgetting the self parameter in instance method definitions or simply passing the wrong number of arguments during a function call.

By carefully checking your method signatures (especially for self) and function calls, you can easily resolve this common Python error. Avoid shadowing built-in names to prevent related issues.