Check if an Argument in a Function Is Passed in Python

Consider the following Python function:

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,

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.

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.

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,

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.

Output:

True
False

That is why the following code yields “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.

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)

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.