In our previous Python tutorial, we have explained How To Use Pandas Library in Python. In this tutorial, we will explain about *Args and **Kwargs in Python.
In programming, we often define function with arguments to perfrom similar operations to make reusable code. Python provides a special feature for passing argument. We can pass a variable number of arguments to a function using special symbols.
What is *args and **kwargs in Python?
Sometime we are unsure about the number of arguments to pass in the functions, in this case we can use special symbols to pass number of aruments. There are two special symbols to pass number of argument in Python.
- *args: Used to pass non keyword arguments.
- **kwargs: Used to pass keyword arguments.
Let’s Play with *Args and **Kwargs in Python
Now we will discuss the use of special symbols *args and **kwargs in real scenario.
Suppose we define a function add()
for adding of 2 numbers.
def add(a, b): return a+b sum = add(10, 20) print ('sum is: ', sum)
When we run the above program, the output will be:
sum is: 30
In above program, we have defined function add()
with two argument a
and b
and get sum of these.
When we pass more than two values while calling add()
function,
def add(a, b): return a+b sum = add(10, 20, 30) print ('sum is: ', sum)
When we run the above program, we get the output as error like:
TypeError: add() takes 2 positional arguments but 3 were given
In above program, we passed 3 arguments to the add()
function instead of 2 arguments and got TypeError.
*args
As in above program, we are not sure about number of argument that can be passed to a function. So we will use *args
solution to overcome this issue. The *args
, that allow to pass the variable number of non keyword arguments to function.
We need to use *
before the parameter name to pass variable length arguments. The arguments are passed as a tuple.
def add(*num): sum = 0 for n in num: sum = sum + n return sum sum1 = add(10, 20, 30) sum2 = add(5, 15, 25, 35, 45, 55, 65, 70) print ('sum1 is: ', sum1) print ('sum2 is: ', sum2)
When we run the above program, the output will be:
sum1 is: 60 sum2 is: 315
In above program, we have used *num
as parameter to define function which allows to pass variable length argument list to the add()
function. We have passed number of arument and then we have a loop on passed argument to add and return sum.
**kwargs
We need to use solution called **kwargs
to pass the variable length of keyword arguments to the function.
The double asterisk **
is used before the parameter name while defining the function to pass the variable length of keyword arguments. The arguments are passed as a dictionary and these arguments make a dictionary inside function with name same as the parameter.
def getEmployee(**data): for key, value in data.items(): print("{} is {}".format(key,value)) getEmployee(Name="Steve Smith", Age=25, Designation="Web Developer", Country="France") getEmployee(Name="John William", Email="john@webdamn.com", Country="Britain", Age=30, Phone=1234567890)
When we run the above program, the output will be:
Name is Steve Smith Age is 25 Designation is Web Developer Country is France Name is John William Email is john@webdamn.com Country is Britain Age is 30 Phone is 1234567890
In above program, we have defined function getEmployee()
and added **
with parameter as **data
. We have called function two times and passed dictionaries with variable length of keyword arguments. We loop through passed dictionary data and prints the value of the dictionary.