Summary: In this tutorial, we will learn what *args and **kwargs are in Python and why we should use them in our programs.
What is the need of *args and **kwargs?
Consider a situation where we have to write multiple functions just for the sake of accepting different numbers of arguments.
For example the following add
method in Python:
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
Note that we have to write two overloaded functions to add different numbers of arguments.
Every time the number of arguments differs, we need to write a specific function corresponding to the same.
One way we can resolve this is by using a list as 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 with arguments, and that is by using *args and **kwargs.
What are *args and **kwargs?
*args
and **kwargs
are keywords in Python which when defined as arguments allows a function to accept a varying number of arguments.
They are useful in cases when we are unsure of the number of arguments for a particular function.
Both of them make a function flexible in terms of arguments, but 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.
A positional argument is a name that is not followed by an equal sign (=) and default value.
def add(*args):
print(sum(args))
add() #0-arguments
add(5, 6, 4) #3-arguments
add(1, 1, 1, 1, 1) #5-arguments
Output:
15
5
As you can notice, *args
can accept a variable number of arguments (even 0 arguments), thus makes a function more flexible.
If we would not have used *args
, we would have to define separate functions for each function call.
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 that transforms the incoming arguments into a Python tuple.
Keeping the *
operator as it is, we can rename the args
to any name like the following:
def add(*integers):
print(sum(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 and at the same time are 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)
Here, we can use **kwargs
to make the function more flexible with respect to keyword arguments as follows:
def student(**kwargs):
print(kwargs['name']) #output: pencil
print(kwargs['roll']) #output: 77
student(name="pencil", roll=77, department="IT")
As you can see, we have passed an additional keyword argument (i.e. department) to the student
function and yet the code works fine.
This is the power of **kwargs
in Python.
Can we Rename **Kargs?
Yes likewise *args
, we can rename kwargs
in **kwargs
.
The important part 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.