*args and **kwargs in Python

Agil C
Analytics Vidhya
Published in
4 min readAug 9, 2020

--

args and kwargs

Sometimes if you check function definition in Python you will see two strange arguments *args and **kwargs, if you have wondered what it is and want to how it works then this article is for you. In Python we can pass a variable number of arguments to a function, *args will help to pass multiple arguments, and **kwargs will allow us to pass keyword arguments to a function.

*args in Python

Normally we will pass arguments to a function like this:

The function which takes 2 arguments and returns their product

but here is a problem, we need to pass exactly 2 number of arguments to get their product, ie, we can’t use this function if we need to find the product of more than two numbers. If we need to find the product of 3 numbers we need to modify the function or write a new function specifically for that purpose. But it is not convenient in the real world, we can’t write n different functions to find the product of all combinations of n numbers!

What is the solution to this problem then🤔,

The most intuitive solution for this will be passing the numbers as a list or set to the function and in the function iterate the list to get the product of all numbers like the code shown below.

Passing a list of number to a function1

Yes, the list implementation will work but it won't be the most efficient solution because whenever we need to call the function we need to the argument as a list. This will be tricky when we don’t know the numbers in advance in that case we need to create the list first then pass it to the function. This is where *args will be useful.

mul_nums function using *args

In the above example, we are passing 4 numbers as positional arguments to mul_nums function. In the function * in front of args will convert the different arguments of the function to a tuple. We can then iterate the tuple to find the product of all the numbers similar to passing a list as an argument. But we don’t have to create a list before passing it to the function instead we can pass all the numbers as positional arguments. The point to note is args is just a variable name here, we can use whatever name we want, * is doing the job of converting the positional arguments into a tuple and assigning it to args.

**kwargs in Python

kwargs also work similar to *args, but here we need to pass named or keyword arguments to the function instead of positional arguments. If we implement the same multiplication function using **kwargs:

mul_nums function using **kwargs

Here the ** operator will convert the named arguments to a Python dictionary and assign it to kwargs. We can iterate through the values of the dictionary and find the product of numbers. Kwargs is just a variable name here we can use whatever name we want as the function argument.

Since the kwargs are a dictionary we have to use values method to get all the values on the dictionary. If we didn’t use the values method in kwargs it will iterate through the dictionary keys, here python will throw an error since the dictionary keys are string and we can’t multiply strings in Python.

Iterating through kwargs without values method

If we want both keys and values in the for loop then we can iterate through kwargs.items as shown below:

Iterating through kwargs.items

Ordering Arguments

There is a certain order we need to keep while passing both positional and keyword arguments to a function,

  1. Standard arguments
  2. Positional arguments(*args)
  3. Keyword arguments(**kwargs)
ordering arguments

Here the function correct_order got executed without any error because we are following the correct ordering of arguments, but in the second function, incorrect_order python thrown an error because **kwargs is used before *args.

Summary

  1. *args and **kwargs are very handy while passing a variable number of arguments to a function.
  2. *args are used to pass positional arguments.
  3. **kwargs are used to pass keyword arguments
  4. While passing different types of arguments the order should be Standard arguments, Positional arguments(*args), and Keyword arguments(**kwargs).

--

--

Agil C
Analytics Vidhya

Startup Enthusiast, Full Stack Engineer, Visit https://agilc.xyz/ for more info