[Solved] TypeError: __init__() missing 2 required positional arguments

If we are instantiating the class that accepts 2 required positional parameters and if we do not pass those 2 required arguments Python interpreter will throw TypeError: __init__() missing 2 required positional arguments

In this tutorial, we will look at what exactly TypeError: __init__() missing 2 required positional arguments and how to resolve this error with examples.

What is TypeError: __init__() missing 2 required positional arguments

Let us take a simple example to demonstrate this issue.

class Student():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def get_full_name(self):
        return " ".join([self.first_name, self.last_name])


# No Positional arguments are passed while instantiating the class
student1 = Student()
print(student1.get_full_name())

Output

TypeError: Student.__init__() missing 2 required positional arguments: 'first_name' and 'last_name'

In the above code, we have a class called Student() and a constructor that takes 2 positional arguments. The class also has a method get_full_name() method that combines the first name, and last name and returns the full name of the Student.

In the following statement, we instantiate the class Student() without passing any required positional arguments, and hence, we get the TypeError: Student.init() missing 2 required positional arguments: ‘first_name’ and ‘last_name’

How to fix TypeError: __init__() missing 2 required positional arguments

There are different ways to resolve this TypeError. Let us look at each of these solutions with examples.

Solution 1 – Pass the required positional arguments

The easy way to resolve the error is to pass the required positional arguments that are specified in the __init__() method while instantiating the class.

Like any other programming language C#, C++, etc Python has its own way of defining the constructor through __init__() method.

The __init__() function is called every time an object is created from a class and it allows the class to initialize the attributes of the class.

If we do not define the __init__() function or the constructor then we do not have to pass any arguments while instantiating the class.

Here in our example, the Student class has __init__() function and it takes two positional arguments, and we can solve the issue by providing the positional arguments while instantiating the class as shown below.

class Student():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    def get_full_name(self):
        return " ".join([self.first_name, self.last_name])


student1 = Student("Chandler", "Bing")
print(student1.get_full_name())

Output

Chandler Bing

Solution 2 – Set the default values for the arguments

In Python, the function arguments can have the default values and we can set the default values in the __init__() function too.

After setting the default values we do not have to pass any argument values while instantiating the class, the default values that are defined in the __init__() will be used instead of throwing a TypeError.

The default values are set using the assignment operator (=). The syntax will be in the form of keyword=value.

Let us see how we can implement the default values in our example and resolve the issue.

class Student():
    def __init__(self, first_name="", last_name=""):
        self.first_name = first_name
        self.last_name = last_name

    def get_full_name(self):
        if not self.first_name and not self.last_name:
            return "The first name and last name is empty"

        else:
            return " ".join([self.first_name, self.last_name])


student1 = Student("Chandler", "Bing")
print(student1.get_full_name())

student2 = Student()
print(student2.get_full_name())

Output

Chandler Bing
The first name and last name is empty

In the above example, we are not passing the required positional arguments while instantiating the class. However, the default values we have set in the __init__() function args are taken.

Here you need to ensure that the default values are set only on the value types and not on the reference types.

The reference types such as Dictionary, List, Array, Tuple, Set, etc., can cause different issues, as shown below.

Notice that the method takes the empty dictionary as the default value when we do not pass any arguments.

Here both emp1 and emp2 objects hold the reference of the address dictionary object, and changing the emp1 object will implicitly change the emp2 object, as demonstrated in the below code.

class Employee():
    def __init__(self, address={}):
        self.address = address

    def get_full_address(self):
        return self.address


emp1 = Employee()
print("Employee 1 address", emp1.get_full_address())
emp1.address["city"] = "Bangalore"

emp2 = Employee()
print("Employee 2 address", emp2.get_full_address())

Output

Employee 1 address {}
Employee 2 address {'city': 'Bangalore'}

As shown below, we can fix this issue by setting the default argument to None and conditionally returning the empty dictionary if the parameter is None.

class Employee():
    def __init__(self, address=None):
        self.address = address
        if address is None:
            self.address = {}

    def get_full_address(self):
        return self.address


emp1 = Employee()
print("Employee 1 address", emp1.get_full_address())
emp1.address["city"] = "Bangalore"

emp2 = Employee()
print("Employee 2 address", emp2.get_full_address())

Output

Employee 1 address {}
Employee 2 address {}

Conclusion

The TypeError: __init__() missing 2 required positional arguments occurs if we do not pass the 2 required positional arguments while instantiating the class.

We can resolve the issue by passing the required positional arguments while instantiating the class or by setting the default values for the arguments using the assignment operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

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