Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

ItsMyCode: [Solved] TypeError: missing 2 required positional arguments

$
0
0

If we have a function that accepts 2 arguments, and while calling the method, if we do not pass those 2 required arguments Python interpreter will throw TypeError: missing 2 required positional arguments

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

What is TypeError: missing 2 required positional arguments

Let us take a simple example to demonstrate this issue.

# Performs Sum of 2 numbers
def calculate_sum(x, y):
    return x+y

# No args are passed 
output = calculate_sum()
print("Sum of 2 numbers is ", output)

Output

TypeError: calculate_sum() missing 2 required positional arguments: 'x' and 'y'

In the above code, we have a method called calculate_sum() that takes 2 positional arguments and returns the sum of 2 numbers.

In the following statement, we call the method calculate_sum() without passing any positional arguments to the method; hence, we get the TypeError: missing 2 required positional arguments.

How to fix TypeError: 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 necessary positional arguments to the method. Here in our example, the function takes two arguments, and we can solve them by providing the values for both x and y args, as shown below.

# Performs Sum of 2 numbers
def calculate_sum(x, y):
    return x+y

# 2 required args are passed
output = calculate_sum(5, 6)
print("Sum of 2 numbers is ", output)

Output

Sum of 2 numbers is  11

Solution 2 – Set the default values for the arguments

In Python, the function arguments can have the default values. If we call the function without passing any argument values, the default values are 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.

# Performs Sum of 2 numbers
def calculate_sum(x=0, y=0):
    return x+y


# No args are passed
output = calculate_sum()
print("Sum of 2 numbers is ", output)

Output

Sum of 2 numbers is  0

In the above example, we are not passing the required positional arguments to the method calculate_sum(). However, the default values we have set in the function args are taken to calculate the sum of 2 numbers.

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 employee dictionary object, and changing the emp1 object will implicitly change the emp2, as demonstrated in the below code.

# Fetch Employee Details
def get_employee_details(employee={}):
    return employee


# No args are passed
emp1 = get_employee_details()
emp2 = get_employee_details()

# Since the emp1 and emp2 object holds a reference of employee object
# changing emp1 will change emp2 object implicitly.
emp1["name"] = "Chandler Bing"

print("Employee 1 details are  ", emp1)
print("Employee 2 details are  ", emp2)

Output

Employee 1 details are   {'name': 'Chandler Bing'}
Employee 2 details are   {'name': 'Chandler Bing'}

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.

# Fetch Employee Details
def get_employee_details(employee=None):
    if (employee is None):
        employee = {}
    return employee


# No args are passed
emp1 = get_employee_details()
emp1["name"] = "Chandler Bing"
emp2 = get_employee_details()


print("Employee 1 details are  ", emp1)
print("Employee 2 details are  ", emp2)

Output

Employee 1 details are   {'name': 'Chandler Bing'}
Employee 2 details are   {}

Conclusion

The TypeError: missing 2 required positional arguments occurs if we do not pass the 2 required positional arguments while calling the function or the method.

We can resolve the issue by passing the required positional arguments to the function or by setting the default values for the arguments using the assignment operator.


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>