
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use args and kwargs in Python
In this article we will discuss about *args and **kwargs properties and their usage in Python.
To pass unspecified number of arguments to a function usually *args and **kwargs properties are used. The *args property is used to send a non-keyword variable length argument to a function call whereas **kwargs keyword is used to pass a keyword length of arguments to a function.
Keyword arguments (or arguments) are the values that is preceded by an identifier "=" in a function call for e.g. "name=".
The primary benefits of using *args and **kwargs are readability and convenience, but they should be used with care.
Note ? It is not necessarily required to write *args and **kwargs. There is only a need for the asterisk (*). For instance, you could write *var and **vars as well.
Usage of*args in Python
To pass a variable number of non-keyword arguments in the function, we need to utilize an asterisk (*) before the parameter name
Example
Following is an example to pass arguments in a function using *args -
def multiply (*args): print(args, type(args)) multiply(6,78)
Output
So, we can be confident that the method that receives these provided parameters creates a tuple with the same name as the parameter excluding *.
(6, 78) <class 'tuple'>
Example
Let's now multiply a variable number of arguments to our multiply() function as shown in the example below -
def multiply(*args): x = 1 for i in args: x = x * i print ('The product of the numbers is:',x) multiply(5,87) multiply(8,11) multiply(6,9,4,7) multiply(2,11,98)
Output
Following is an output of the above code -
The product of the numbers is: 435 The product of the numbers is: 88 The product of the numbers is: 1512 The product of the numbers is: 2156
Note ? The name of the argument need not be args; it might be anything. It's numbers in this instance. The use of the name *args is, however, commonly accepted.
Usage of **kwargs in Python
We can provide a Python function with any number of keyword arguments by using **kwargs.
To indicate this kind of argument in the function, we use a double asterisk (**) before the parameter name shows that a dictionary of keyworded arguments with variablelength exists.
Example
Following is an example to pass arguments in a function using **kwargs -
def gadgets_price(**kwargs): print(kwargs,type(kwargs)) gadgets_price(laptop= 60000, smartphone=10000, earphones =500)
Output
The arguments are passed as a dictionary and create a dictionary inside the function with the same name as the parameter, except **, as can be seen.
{'laptop': 60000, 'smartphone': 10000, 'earphones': 500} <class 'dict'>
Example
Let's finish the gadgets_price () function now to return the total amount of all the gadgets as shown in the following example:
def gadgets_price(**items): total = 0 for price in items.values(): total += price return total print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, smartphone=10000, earphones =500))) print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, smartphone=10000, desktop =45768))) print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, TV=85965)))
Output
Following is an output of the above code -
The net amount of the gadgets is: 70500 The net amount of the gadgets is: 115768 The net amount of the gadgets is: 145965
Note ? The name of the argument need not be kwargs; it might be anything. It is items in this instance. But using **kwargs as the name is typically accepted.
Using *args and **kwargs in function call
We can pass arguments into functions using *args and **kwargs.
Example
There are three parameters identified in the function as ?laptop', ?smartphone' and ?earphone'. Each of these arguments will be printed out by the function. Then, using the asterisk syntax, we may provide a variable into the function that is set to an iterable (in this case, a tuple) as shown in the example below -
def gadgets(laptop, smartphone, earphone): print('The first gadget is of:',laptop) print('The second gadget is of:',smartphone) print('The third gadget is of:',earphone) args = ('Lenovo','Samsung','JBL') gadgets(*args)
Output
Using the python gadgets.py command, we can run the program and get the output shown below -
The first gadget is of: Lenovo The second gadget is of: Samsung The third gadget is of: JBL
Note - The above program can also be changed to use an iterable list data type and an alternative variable name. Let's also combine the named parameter with the *args syntax:
def gadgets_price(laptop, smartphone, earphone): print('The first gadget is of price:',laptop) print('The second gadget is of price:',smartphone) print('The third gadget is of price:',earphone) list = [15438,499] gadgets_price(54000,*list)
The output of the above code is as follows -
The first gadget is of price: 54000 The second gadget is of price: 15438 The third gadget is of price: 499
Example
Similarly, you can call a function with the keyworded **kwargs parameters. We'll create a variable named kwargs, which will represent a dictionary with three key-value pairs, but you can call it anything you like, and provide it to a function with three arguments as shown below -
def gadgets_price(laptop, smartphone, earphone): print('The first gadget is of price:',laptop) print('The second gadget is of price:',smartphone) print('The third gadget is of price:',earphone) kwargs = {'laptop':54678, 'smartphone':15893, 'earphone':499} gadgets_price(**kwargs)
Output
Using the python some gadgets_price.py command, we can run the program and get the output shown below -
The first gadget is of price: 54678 The second gadget is of price: 15893 The third gadget is of price: 499