TypeError: missing 2 required positional arguments (Python)

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by iam_os

TypeError: missing 2 required positional arguments (Python) #

The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.

typeerror missing 2 required positional arguments

Here is an example of how the error occurs.

main.py
# 👇️ takes 2 arguments - `a` and `b` def do_math(a, b): return a + b # ⛔️ TypeError: do_math() missing 2 required positional arguments: 'a' and 'b' result = do_math()

The do_math function takes 2 positional arguments - a and b, but we called the function without providing values for the arguments.

One way to solve the error is to provide a value for the arguments.

main.py
def do_math(a, b): return a + b result = do_math(10, 10) print(result) # 👉️ 20

We passed values for the arguments to the function which solves the error.

If we define a function that takes 2 arguments, we have to provide values for the arguments when the function is called.

An alternative solution is to set default values for the arguments.

main.py
def do_math(a=0, b=0): return a + b result = do_math() print(result) # 👉️ 0

We set 0 as the default value for the 2 arguments. This could be any other value, e.g. an empty string ("") or even None if you can't think of a suitable default value.

If the function gets invoked without providing values for the a and b arguments, the default values are used.

If you are simply hard coding values for these arguments, you can remove the arguments and declare variables in the body of the function.

main.py
def do_math(): a = 100 b = 200 return a + b result = do_math() print(result) # 👉️ 300

We hardcoded the values for a and b.

An important note is to avoid setting default values for non-primitive arguments, e.g. dictionaries and lists.

Here is an example of how this can go wrong.

main.py
def get_address(address={}): return address addr1 = get_address() addr2 = get_address() addr1['country'] = 'Germany' print(addr1) # 👉️ {'country': 'Germany'} print(addr2) # 👉️ {'country': 'Germany'}

We called the get_address() function 2 times and stored the results in variables.

Notice that we only set the country key on one of the dictionaries, but both of them got updated.

This is because default arguments are only evaluated once - when the function is defined.

They are not evaluated each time the function is called.

When a non-primitive default argument like a dictionary or list is mutated, it is mutated for all function calls.

One way to get around this issue is to set the default argument to None and conditionally update its value in the body of the function.

main.py
def get_address(address=None): if address is None: address = {} return address addr1 = get_address() addr2 = get_address() addr1['country'] = 'Germany' print(addr1) # 👉️ {'country': 'Germany'} print(addr2) # 👉️ {}

The body of the function is ran every time it is invoked, so the issue no longer exists.

Use the search field on my Home Page to filter through my more than 1,000 articles.