TypeError: takes 2 positional arguments but 3 were given

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. TypeError: takes 2 positional arguments but 3 were given
  2. TypeError: takes 1 positional argument but 2 were given

# TypeError: takes 2 positional arguments but 3 were given

The Python "TypeError: takes 2 positional arguments but 3 were given" occurs for multiple reasons:

  1. Forgetting to specify the self argument in a class method.
  2. Forgetting to specify a third argument in a function's definition.
  3. Passing three arguments to a function that only takes two.
  4. Overriding a built-in function by mistake.

typeerror takes 2 positional argument but 3 were given

Here is an example of how the error occurs.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ Forgot to specify `self` arg def increase_salary(a, b): return a + b emp = Employee() # โ›”๏ธ TypeError: Employee.increase_salary() takes 2 positional arguments but 3 were given result = emp.increase_salary(100, 100)

We forgot to specify the self argument in the definition of the increase_salary class method.

# Python automatically passes self to the class method

Python automatically passes self to the class method when it is called, so a method that takes 2 arguments gets passed 3 arguments which causes the error.

main.py
class Employee(): # โœ… Specify self as the first arg def increase_salary(self, a, b): return a + b emp = Employee() result = emp.increase_salary(100, 100) print(result) # ๐Ÿ‘‰๏ธ 200

python automatically passes self

self represents an instance of the class, so when we assign a variable as

Specifying the self argument in the method's definition solves the error.

# If you don't use the self argument, declare a static method

If your method doesn't make use of the self argument, declare a static method.

main.py
class Employee(): @staticmethod def increase_salary(a, b): return a + b emp = Employee() result = emp.increase_salary(100, 100) print(result) # ๐Ÿ‘‰๏ธ 200 result = Employee.increase_salary(100, 100) print(result) # ๐Ÿ‘‰๏ธ 200

declare static method if you dont need self

A static method doesn't receive an implicit first argument and can be called on the class or on an instance of the class.

# Forgetting to specify the third argument in a function's definition

The error is also caused when you forget to specify the third argument in a function's definition or pass 3 arguments to a function that only takes 2.

main.py
def do_math(a, b): return a + b # โ›”๏ธ TypeError: do_math() takes 2 positional arguments but 3 were given do_math(10, 20, 30)

The do_math function takes two arguments but it gets called with 3.

In this situation, we either have to update the function's declaration and take a third argument or remove the third argument from the function call.

Here is an example of removing the argument from the function call.

main.py
def do_math(a, b): return a + b result = do_math(10, 20) print(result) # ๐Ÿ‘‰๏ธ 30

And, here is an example of specifying the third argument in the function's definition.

main.py
def do_math(a, b, c): return a + b + c result = do_math(10, 20, 30) print(result) # ๐Ÿ‘‰๏ธ 60

Make sure you aren't overriding any built-in functions or classes by declaring a function with the same name as that can also cause the error.

# TypeError: takes 1 positional argument but 2 were given

The solutions to the "TypeError: takes 1 positional argument but 2 were given" error are the same.

The Python error occurs for multiple reasons:

  1. Forgetting to specify the self argument in a class method.
  2. Forgetting to specify a second argument in a function's definition.
  3. Passing two arguments to a function that only takes one.
  4. Overriding a built-in function by mistake.

Here is an example.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ forgot to specify `self` arg def get_name(name): return name emp = Employee() # โ›”๏ธ TypeError: Employee.get_name() takes 1 positional argument but 2 were given print(emp.get_name('Alice'))

typeerror takes 1 positional argument but 2 were given

Specify the self argument in the definition of the method to solve the error.

main.py
class Employee(): # โœ… Specify self as the first arg def get_name(self, name): return name emp = Employee() print(emp.get_name('Bobby')) # ๐Ÿ‘‰๏ธ "Bobby"

specify self argument

If your method doesn't make use of the self argument, declare a static method.

main.py
class Employee(): @staticmethod def get_name(name): return name emp = Employee() print(emp.get_name('Bobby')) # ๐Ÿ‘‰๏ธ "Bobby" print(Employee.get_name('Bobby')) # ๐Ÿ‘‰๏ธ "Bobby"

declaring static method

A static method does not receive an implicit first argument and can be called on the class or on an instance of the class.

The error is also caused when you forget to specify the second argument in a function's definition or pass 2 arguments to a function that only takes 1.

main.py
# โ›”๏ธ TypeError: do_math() takes 1 positional argument but 2 were given def do_math(a): return a * a result = do_math(5, 10)

The do_math function takes a single argument but it gets called with 2.

In this situation, we either have to update the function's declaration and take a second argument or remove the second argument from the function call.

Here is an example of removing the argument from the function call.

main.py
def do_math(a): return a * a result = do_math(5) print(result) # ๐Ÿ‘‰๏ธ 25

And, here is an example of specifying the second argument in the function's definition.

main.py
def do_math(a, b): return a * b result = do_math(5, 10) print(result) # ๐Ÿ‘‰๏ธ 50

Make sure you aren't overriding any built-in functions or classes by declaring a function with the same name because that also causes the error.

# Conclusion

To solve the Python "TypeError: takes 2 positional arguments but 3 were given", make sure:

  1. You haven't forgotten to specify the self argument in a class method.
  2. You haven't forgotten to specify a third argument in a function's definition.
  3. You haven't passed 3 arguments to a function that only takes 2.
  4. You aren't overriding a built-in method.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev