Summary: In this tutorial, we will learn what *args and **kwargs are in Python and why we should use them in our programs.

While write programs, we define overload a function to accept different numbers or arguments.

Consider the following Python code for instance:

def add(a, b):
    return a+b

def add(a, b, c, d, e, f):
    return a+b+c+d+e+f

print(add(5,6))                #output 11
print(add(5, 6, 1, 5, 2, 2))   #output 21

Notice that we have to write two functions to add different numbers of arguments. Every time the number of arguments differs, we have to write a specific function corresponding to the same.

One way to resolve this is to use a list as single parameter to accept multiple integers like the following:

def add(lst):
    return sum(lst)

print(add([5, 6, 1, 5, 2, 2]))   #output 21

Though this solves the problem, it still changes the way we wanted to use the function.

There is another way using which we can make a function in Python more flexible in terms of the number of arguments is, and it is by using *args or **kwargs.

What are *args and **kwargs?

*args and **kwargs are syntaxes in Python that can used as an argument in a function.

If defined in the declaration of the function, they can accept varying number of arguments.

They are useful in cases when we are unsure of the number of arguments for a particular function.

However they slightly differ from each other. Let see examples of both.

*args in Python

*args in Python is a Non-Keyword Argument that can accept and store a variable number of positional arguments as a tuple.

positional argument is a name that is not followed by an equal sign (=) and default value.

def add(*args):

add()                #0-arguments
add(5, 6, 4)         #3-arguments
add(1, 1, 1, 1, 1)   #5-arguments



As you can notice, *args can accept a variable number of arguments (even 0 arguments), thus making a function more flexible.

Can we Rename *args?

Yes, we can. This is because args in *args is just a name, what is more important is the operator *.

The * in *args is an unpacking operator which is responsible for transforming the incoming arguments into a tuple.

Keeping the * prefixed, we can rename the args to any name like the following:

def add(*integers):
add(-1, 7, 2, 6)    #output 14

**kwargs in Python

**kwargs in Python is a Keyword Argument that can accept and store the keyword arguments of varying types as a dictionary.

A keyword argument (also known as named argument) is followed by an equal sign and an expression that gives its default value.

It is useful in cases when we want a function to accept a varying number of arguments, but is also uncertain of their types.

Consider the following code, where a function accepts two different keyword arguments of varying types:

def student(name="", roll=-1):
    print(name)    #output: pencil
    print(roll)    #output: 77
student(name="pencil", roll=77)

We can **kwargs in this case to make the function more flexible with respect to keyword arguments like the following:

def student(**kwargs):
    print(kwargs['name'])   #output: pencil
    print(kwargs['roll'])   #output: 77

student(name="pencil", roll=77, department="IT")

As you can notice, we have passed an additional keyword argument (i.e. department) to the function but still, the function remains unaffected. This is the power of **kwargs in Python.

Can we Rename **Kargs?

Yes likewise *args, we can rename kwargs in **kwargs.

The important past is the ** (unpacking operator) which should be prefixed with the name.

Note: we can use any names instead of args and kwargs, what makes them meaningful is * (single asterisk) to make it Non-Keyword Argument and ** (Double asterisks) to make it Keyword Argument.

Hope now you got little idea about *args and **kwargs in python and will be able to use it in your programs. Any doubts then comment below.

Leave a Reply

4 × 5 =