Skip to main content

python-error-typeerror-takes-0-positional-arguments-but-1-was-given

How to Solve "TypeError: takes 0 positional arguments but X were given" in Python

The TypeError: takes 0 positional arguments but X were given error (where X is a number like 1, 2, etc.) occurs in Python when you call a function or method that doesn't accept any arguments, but you provide one or more arguments during the call.

This guide explains the common causes of this error and provides clear solutions, including correct function/method definitions and calls, and avoiding name conflicts.

Understanding the Error

This error is very direct:

  • takes 0 positional arguments: The function or method you're calling was defined in a way that it doesn't accept any positional arguments.
  • but X were given: You tried to call the function or method with X number of arguments.

Python functions and methods must explicitly define the arguments they accept. If you define a function with no parameters, and then try to call it with parameters, you'll get this error.

Common Causes and Solutions

Missing Parameters in Function Definition

The most basic cause is a mismatch between how you defined the function and how you're calling it:

def do_math():  # No parameters defined
return # You should return some value

# ⛔️ TypeError: do_math() takes 0 positional arguments but 2 were given
# result = do_math(10, 15)

# ✅ Correct
def do_math(a, b): # Defined with parameters
return a + b
result = do_math(10, 15)
print(result)
  • Solution: Modify the function definition to accept the arguments you're trying to pass. Add parameter names within the parentheses: def do_math(a, b):.
  • Alternatively, if the function truly shouldn't take any arguments, remove the arguments from the function call: result = do_math().

Incorrect Method Calls on Classes (Missing self)

When working with classes, instance methods (the most common kind) always need to take self as the first parameter:

class Employee():
# Here we forgot to take the self arg
def get_name(): # Forgot self parameter
return 'Tom Nolan'


emp = Employee()

# ⛔️ TypeError: Employee.get_name() takes 0 positional arguments but 1 was given
#print(emp.get_name())

# ✅ Correct
class Employee():
def get_name(self):
return 'Tom Nolan'
emp = Employee()
print(emp.get_name()) # Output: Tom Nolan
  • self: Represents the instance of the class on which the method is called. When you call emp.get_name(), Python automatically passes the emp object as the self argument.
  • Solution: Always include self as the first parameter in instance methods.

Static Methods

If your class method doesn't use the self parameter, you should use the @staticmethod decorator, or make sure that the method takes parameters:

class Employee():
@staticmethod
def get_name(): # Static method, with no parameters
return 'Tom Nolan'
emp = Employee()
print(emp.get_name()) # Output: Tom Nolan

# Or

class Employee():
def get_name(self, name): # Instance method, with name parameter.
return name

emp = Employee()
print(emp.get_name('Tom Nolan')) # Output: Tom Nolan

Overriding Built-in Functions

Never define your own functions or variables with the same names as built-in Python functions (like str, list, int, open, etc.). If you do, you'll "shadow" the built-in, and you'll get very confusing errors:

def str():  # ⛔️ NEVER DO THIS!  You've overridden the built-in str()
return 'tutorialreference.com'

# ⛔️ TypeError: str() takes 0 positional arguments but 1 was given
# result = str(123)
  • Solution: Rename your function to something unique. Never use names that conflict with built-in Python functions or types.

Conclusion

The TypeError: takes 0 positional arguments but X were given error is a fundamental error in Python, and is caused by a mismatch between the function's/method's definition and it's call.

By double-checking your function definitions, ensuring you're including self in instance methods, and avoiding names that conflict with built-ins, you can easily resolve this error.