How To Solve “TypeError: missing 2 required positional arguments” in Python

TypeError: missing 2 required positional arguments in Python happens when you have not assigned values to the necessary parameters. To fix this Error, you have to assign enough values to necessary parameters or set the default values of parameters when creating an object. Let’s learn more about it with the explanation and example below.

How Does The TypeError: missing 2 required positional arguments” in Python Happen?

The TypeError: missing 2 required positional arguments in Python happens when you do not assign enough necessary parameters to a function or a class. 

Let’s see how this Error occurs.

class Blogger:
    def __init__(self, name, university):
        self.name = name
        self.university = university

    def setAgeAndCountry(self, age, country):
        self.age = age
        self.country = country

blogger = Blogger() # This statement will make an Error
blogger.setAgeAndCountry() # This statement will make an Error

Output

TypeError: Blogger.__init__() missing 2 required positional arguments: 'name' and 'university'

This code got the Error because I didn’t assign values for the ‘name’ and ‘university’ parameters.

How To Solve The TypeError: missing 2 required positional arguments in Python

To solve this Error, you have to assign enough values to the necessary parameters or set the default values for the parameters in a method.

Assign enough values to the necessary parameters

In this solution, you need to assign enough values to the necessary parameters.

class Blogger:
    def __init__(self, name, university):
        self.name = name
        self.university = university

    def setAgeAndCountry(self, age, country):
        self.age = age
        self.country = country

    def __str__(self):
        return self.name + ' ' + str(self.age) + ' ' + self.university + ' ' + self.country

# Create an object
blogger = Blogger('crvt4722', 'PTIT')

# Set the age and country of this blogger
blogger.setAgeAndCountry(20, 'Vietnam')
print(blogger)

Output

crvt4722 20 PTIT Vietnam

Set the default value for the parameters

Another solution to prevent this Error is setting the default value for the parameters of the method. In this way, you can assign the values to the parameters when calling the method or not.

Look at the example below.

class Blogger:
    def __init__(self, name='Cristiano', university=''):
        ''' 
        By default, if you do not assign values to the 'name' and 'university' parameters, 
        the name will be 'Cristiano' and the university is an empty String.
        '''

        self.name = name
        self.university = university

    def setAgeAndCountry(self, age=37, country='Portugal'):
        ''' 
        By default, if you do not assign values to 'age' and 'country' parameters, 
        the name will be 37, and the university is 'Portugal'.
        '''
        self.age = age
        self.country = country

    def __str__(self):
        return self.name + ' ' + str(self.age) + self.university + ' ' + self.country

# Create an object
blogger = Blogger()

# Set the age and country of this blogger
blogger.setAgeAndCountry()
print(blogger)

Output

Cristiano 37 Portugal

Summary

We have shown you how the TypeError: missing 2 required positional arguments in Python happens and how to solve it in two ways. You should use the second solution to prevent the Error even if you do not assign values to the parameters. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

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