Check if an Argument in a Function Is Passed in Python

Consider the following Python function:

def func1(val1, val2, val3):
    return val1 * val2 + val3

# Calling the function.
result = func1(3, 4, 6)

The function, func1 takes three arguments: val1, val2 and val3. In this article, we want to learn how to check if a given argument is passed during the function call.

For example, how do we know if the user passed the val3 argument value?

We will use the following methods:

  1. Method 1: Using default argument value(s) and if-statement,
  2. Method 2: Using **kwargs in function definition if-statement

Method 1: Using default argument values and condition statement

The idea is to set a default value for the argument we want to check and then use the if statement to check if the user passed a different value at the function call. Here is an example,

# The function func1_a defined a default value for val3.
def func1_a(val1, val2, val3=7):
    if val3 != 7:
        # If the user explicitly passed passes val3, then the default value,
        # val3=7, is not used. The value passed by the user takes precedence.
        print("val3 argument was explicitly passed by the user.")
    else:
        print("val3 argument was not passed. Default used.")
    print("val3 is :", val3)

# Calling func1_a with three positional arguments,
# 3, 4, 6 for val1, val2, val3, respectively.
func1_a(3, 4, 6)
func1_a(3, 4)

Output:

val3 argument was explicitly passed by the user.
val3 is : 6
val3 argument was not passed. Default used.
val3 is : 7

In the example above, val3=7 is used as a default value. If the user passes val3 in the function call, the value passed will take precedence over the default. In the first function call, val3=6 is passed, but in the second call,val3 is not passed, and therefore, the default value is used.

But what if the user explicitly passes val3=7 on the function call? That will be the same as the default; therefore, our test will fail, as shown in the example below.

func1_a(4, -3, 7)

Output:

val3 argument was not passed. Default used.
val3 is : 7

Important: The idea is to pass a default value that is not expected to be passed by the user. The convention is to use None, as shown in the example below.

def func1_b(val1, val2, val3=None):
    if val3 is not None:
        # if val3 is not None, then it means the argument was passed.
        print("val3 argument was passed during the function call.")
    else:
        # if val3 is None, then it means the default value is being used.
        # Argument not passed by the user.
        print("val3 was not passed to the function. Default value is used.")
    print("val3 is: ", val3)

func1_b(3, 4, 6)
func1_b(5, 6)

Output:

val3 argument was passed during the function call.
val3 is:  6
val3 was not passed to the function. Default value is used.
val3 is:  None

In some cases, however, None could be a valid input for your arguments. In such a case, you can use object() as a default value for the argument you want to check. For example,

# Provide default and check it using if-statement.
default_value = object()

def func2_b(val1, val2, val3=default_value):
    if val3 is default_value:
        print("val3 argument was not passed during the function call.")
    else:
        print("val3 was passed to the function")
    print("val3 is: ", val3)

func2_b(2, 4)
func2_b(5, 6, 8)

Output:

val3 argument was not passed during the function call.
val3 is:  <object object at 0x7f5579e9a4d0>
val3 was passed to the function
val3 is:  8

Note: Unlike the first two examples, you need to define the object() default outside the function definition. This is because Every time you execute the object() function, you get a unique instance. That is why object() != object(). See below.

print(None==None)
print(object()==object())

Output:

True
False

That is why the following code yields “Fail”.

val3 = object()
if val3 is object():
    print("Pass")
else:
    print("Fail")

Method 2: Using **kwargs in function definition if-statement

The **kwargs attribute is used to pass a variable number of keyword arguments. When arguments are passed on a function call, the arg-value pairs of those arguments are stored on **kwargs – a dictionary data structure.

Here is how to use **kwargs to check whether an argument is passed.

def func1(val1, **kwargs):
    # **kwargs is a convenience dictionary containing argument-value pairs
    # (argument names are the keys and values are the argument values)
    # **kwargs will hold all keyword arguments except val1, which is defined explicitly.
    print(kwargs)
    if "val3" in kwargs:
        # If val3 is found in kwargs, it means it was passed during the function call.
        param = kwargs["val3"]
        print("val3 argument was passed.")
    else:
        print("val3 was not passed.")
    print("val3 is: ", kwargs.get("val3"))


# Passing 4 arguments to func1
func1(val1=3, val2=4, val3=5, val4=6)

Output:

{'val2': 4, 'val3': 5, 'val4': 6}
val3 argument was passed.
val3 is:  5

Let’s call the same function by passing 3 arguments to it (excluding val3 – the argument we are checking)

# Passing 3 arguments to func1
func1(val1=3, val2=4, val4=6)

Output:

{'val2': 4, 'val4': 6}
val3 was not passed.
val3 is:  None

Conclusion

This article discussed two methods of checking if an argument in a function is passed. The first method discussed using a default value, and the second explained how to use **kwargs when checking if an argument was passed at a function call.