*args and **kwargs are special syntaxes in python. You must have come across them if you have been using python for a while. If you haven’t, you might encounter them soon. In this post, we will see why they are special and what they are used for.

What is *args ?

In general, when defining a function, we fix the number of arguments that can be passed into it.

The multiply function must take two arguments. If we pass less than two, or more than two arguments, we get an error.

What if while defining the method, we don’t know the number of arguments that it is going to receive?

This is where *args come in.

*args, in Python, can be used as a parameter in a function. It allows us to pass any number of arguments to the function.

Let’s modify the previous function:

Let’s run these now

Output:

Wow, we can pass any number of variable to that function and it works. Isn’t that cool?

Note that the object args is a tuple and not a list. To verify, let’s print the type of the args object using the type() method.

We get the output:

<class 'tuple'>

Well, can’t I just use list as the parameter?

Yes, in fact a list might be a better alternative here because it would tell the reader explicitly to expect a list of items to be multiplied.

Now, let’s consider another scenario.

I now have a wrapper function for calling the operation functions based on the value of first argument. The wrapper function, ‘perform_operation’, calls the appropriate operation function (sum or multiply) based on the value of first argument.

Output:

9
10

We did not unwrap the args in perform_operation. Instead, we passed it directly to the ‘sum’ or ‘multiply’ function without unpacking it. The perform_operation function did not have to know what the args contained. However, it did check the value of the first argument, the operation variable.

*args in print() function

Have you noticed how the print() function works in python?

Try checking the output of the following:

See how print() function can accept any number of arguments? Although the function is implemented in C, this was something achieved by making use of the *args. You can see the source code here.

What is **kwargs?

Similar to *args, **kwargs also allows us to pass variable-length arguments to the function. However, **kwargs is for key-word arguments. Unpacking **kwargs, gives us a dictionary.

Output:

{'first_key': 'value1', 'second_key': 'value2', 'third_key': 'value3'}

Now, let’s see another example for a program to perform addition or multiplication on two numbers:

Output:

9
20

Ordering

While using *args and **kwargs, it is important to keep in mind the ordering that the python requires. Arguments follows this order:

  1. Normal non-keyword arguments
  2. *args
  3. Normal key-word arguments
  4. **kwargs

If all the above types of arguments are used, then the function definition should look like this:

Key things to remember:

  • *args is used for variable-length non-keyword arguments, while **kwargs are for variable-length keyword arguments
  • We get a tuple when unpacking *args, while unpacking **kwargs gives us a dictionary.
  • Keep in mind the order of the arguments:
    i. Normal non-keyword arguments
    ii. *args
    iii. Normal key-word arguments
    iv. **kwargs
  • *args and **kwargs are best used in functions where the number of arguments are small

*****

If you have any questions or comments regarding the post (or something else), please feel free to reach out through the comments.