Last updated: Apr 8, 2024
Reading timeยท4 min

The Python "TypeError: takes 2 positional arguments but 3 were given" occurs for multiple reasons:
self argument in a class method.
Here is an example of how the error occurs.
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.
self to the class methodPython 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.
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

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.
self argument, declare a static methodIf your method doesn't make use of the self argument, declare a
static method.
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

A static method doesn't 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 third argument in a function's definition or pass 3 arguments to a function that only takes 2.
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.
Here is an example of removing the argument from the function call.
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.
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.
The solutions to the "TypeError: takes 1 positional argument but 2 were given" error are the same.
The Python error occurs for multiple reasons:
self argument in a class method.Here is an example.
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'))

Specify the self argument in the definition of the method to solve the error.
class Employee(): # โ Specify self as the first arg def get_name(self, name): return name emp = Employee() print(emp.get_name('Bobby')) # ๐๏ธ "Bobby"

If your method doesn't make use of the self argument, declare a
static method.
class Employee(): @staticmethod def get_name(name): return name emp = Employee() print(emp.get_name('Bobby')) # ๐๏ธ "Bobby" print(Employee.get_name('Bobby')) # ๐๏ธ "Bobby"

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.
# โ๏ธ 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.
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.
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.
To solve the Python "TypeError: takes 2 positional arguments but 3 were given", make sure:
self argument in a class method.You can learn more about the related topics by checking out the following tutorials: