Python Takes 2 Positional Arguments But 3 Were Given

The python takes 2 positional arguments but 3 were given error is a type of TypeError that can occur due to multiple reasons. In this guide, we will discuss the reasons why Python takes 2 positional arguments but 3 were given error is raised and solutions to resolve the error.

What is Python takes 2 positional arguments but 3 were given error?

The python takes 2 positional arguments but 3 were given error is a type of TypeError. This type of error usually arises when in a code two positional arguments are taken when three positional arguments are provided in the code.

Syntax: –

class Employee():
    def increase_salary(a, b):
        return a + b


emp = Employee()

result = emp.increase_salary(100, 100, 100)

Why Python takes 2 positional arguments but 3 were given error occurs?

Python takes 2 positional arguments but 3 were given TypeError may occur due to one of the many reasons listed below.

  1. Overriding a built-in function.
  2. Specification of the self argument in a class method is missing
  3. Specification of a third argument in a function’s definition is missing
  4. Passing three arguments to a function that only takes two

How to resolve Python takes 2 positional arguments but 3 Error?

To solve the Python takes 2 positional arguments but 3 TypeError you need to tackle the reasons mentioned above. To do so you can follow the appropriate methods listed below.

Overriding a built-in function

In Python, overriding is a feature of an object-oriented programming language. In this process, the subclass of the program provides the program with specific characteristics or specific implementation processes of data already defined in the superclass. The execution for Overriding depends on the data used in order to invoke the method and not the reference data already provided in the parent class. It is a crucial concept for OOP as it allows inheritance to use its full power.

Specification of the self argument in a class method

One of the reason for the Python takes 2 positional arguments but 3 TypeError is the missing of ‘self argument in a class. If the ‘self’ argument is specified it will resolve the error. When the ‘self’ argument is called it automatically passes it to the class method. So when a ‘self’ argument is not specified from the code it will not pass it to the class method. But if the ‘self’ argument is specified in the code it will not show the python takes 2 positional arguments but 3 were given error.

Syntax: –

class Employee():
    def increase_salary(self, a, b):
        return a + b

emp = Employee()
result = emp.increase_salary(10, 10)
print(result) 

Using a static method

This method is used if you do not want to use the ‘self’ argument then you can declare a static method in your code. By using a static method even if it does not receive a first argument it can still be called on the class or on an instance of the class instead of the ‘self’ argument.

Syntax: –

class Employee():

    def increase_salary(a, b):
        return a + b


emp = Employee()

result = emp.increase_salary(10, 10)
print(result) 

result = Employee.increase_salary(10, 10)
print(result) 

This method is an alternate method if you do not want to use the ‘sekf’ argument method.

Specification of a third argument in a function’s definition

Another method to solve the Python takes 2 positional arguments but 3 were given TypeError is to specify a third argument in a function’s definition when it is missing from the function’s definition.

Syntax: –

class Employee():

    def increase_salary(a, b, c):
        return a + b + c


emp = Employee()

result = emp.increase_salary(10, 10, 20)

print(result) 

Avoid passing three arguments to a function that only takes two

Some function classes in Python can only allow two arguments at a time. In those arguments if three arguments are taken instead of two it will show the python takes 2 positional arguments but 3 were given TypeError. Therefore you need to avoid passing three arguments to a function that only takes two.

FAQs

What is a positional argument?

A positional argument is a type of argument through which values are passed during a function call. These arguments should be in the order of parameters in the function definition. It is also mandatory for Keyword arguments to follow the positional arguments.

Syntax: –

def add(a,b,c):
    return (a+b+c)
print (add(10,20,30))

What is the self in python?

The ‘self’ in Python is a class of instances that can be used to access the attributes and methods of the class and is also used to bind the attribute with a given argument. The ‘self’ in python is also used to refer to instance attributes and to point at the current object.

Conclusion

This guide here has all the information that will help you to understand the python takes 2 positional arguments but 3 were given TypeError and will also help you to resolve the error with the solutions listed above.

References

  1. Typeerror

Follow us at PythonClear to learn more about solutions to general errors one may encounter while programming in Python.

Leave a Comment