TypeError: method() takes 1 positional argument but 2 were given

If you define a method inside a class, you should add self as the first argument. If you forget the self argument, then Python will raise TypeError: method() takes 1 positional argument but 2 were given

In this tutorial, we will look at what method() takes 1 positional argument but 2 were given error means and how to resolve this error with examples.

TypeError: method() takes 1 positional argument but 2 were given

In Python, we need to pass “self” as the first argument for all the methods which is defined in a class. It is similar to this in JavaScript.

We know that class is a blueprint for the objects, and we can use the blueprints to create multiple instances of objects.

The self is used to represent the instance(object) of the class. Using this keyword, we can access the attributes and methods of the class in Python.

Let us take a simple example to reproduce this error.

If you look at the below example, we have an Employee class, and we have a simple method that takes the name as a parameter and prints the Employee ID as output.

# Employee Class
class Employee:
    # Get Employee method without self parameter
    def GetEmployeeID(name):
        print(f"The Employee ID of {name} ", 1234)

# instance of the employee
empObj = Employee()
empObj.GetEmployeeID("Chandler Bing")

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 10, in <module>
    empObj.GetEmployeeID("Chandler Bing")
TypeError: Employee.GetEmployeeID() takes 1 positional argument but 2 were given

When we run the code, we get a TypeError: method() takes 1 positional argument but 2 were given

How to fix TypeError: method() takes 1 positional argument but 2 were given

In our above code, we have not passed the self argument to the method defined in the Employee class, which leads to TypeError.

As shown below, we can fix the issue by passing the “self” as a parameter explicitly to the GetEmployeeID() method.

# Employee Class
class Employee:
    # Get Employee method with self parameter
    def GetEmployeeID(self,name):
        print(f"The Employee ID of {name} ", 1234)

# instance of the employee
empObj = Employee()
empObj.GetEmployeeID("Chandler Bing")

Output

The Employee ID of Chandler Bing  1234

In Python, when we call the method with some arguments, the corresponding class function is called by placing the methods object before the first argument.

Example object.method(args) will become Class.method(obj,args).

The calling process is automatic, but it should be defined explicitly on the receiving side. 

This is one of the main reasons the first parameter of a function in a class must be the object itself. 

It is not mandatory to use “self” as an argument; instead, we can pass anything over here.

 The “self” is neither a built-in keyword nor has special meaning in Python. It is just a better naming convention that developers use and improves the readability of the code.

Conclusion

The TypeError: method() takes 1 positional argument but 2 were given occurs if we do not pass the “self” as an argument to all the methods defined inside the class.

The self is used to represent the instance(object) of the class. Using this keyword, we can access the attributes and methods of the class in Python.

The issue is resolved by passing the “self” as a parameter to all the methods defined in a class.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python String Center()

Python String center()

Table of Contents Hide center() Syntaxcenter() Parameter center() Return ValueExample 1: center() Method With Default fillcharExample 2: center() Method With * fillchar Example 3: Returns original string if the width is…
View Post
Numpy Repeat

NumPy Repeat

Table of Contents Hide NumPy Repeat SyntaxNumPy Repeat ParametersNumPy Repeat Return ValueExample 1: Repeating the Single Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array…
View Post
Python Dir()

Python dir()

Table of Contents Hide dir() Syntax dir() Parametersdir() Return ValueExample 1: How dir() works?Example 2: When no parameters are passed to dir() method with and without importing external libraries.Example 3: When a module…
View Post