Introduction
People who are new to Python programming find it hard to understand two types of function arguments it provides namely *agrs and **kwargs. Let me be clear about one thing, you need not necessarily name those arguments as by the same name as above (args and kwargs) you can name that argument as your choice(eg- *agr, *agrs) what is important is that asterisk(*) symbol.
Note: for demonstration, I will be using Jupyter Notebook
*args in Python with examples
Example 1
def fun(*args): print(args) fun(1,2,3,4,5,'5.5', "rs")
Output:
(1, 2, 3, 4, 5, '5.5', 'rs')
Using *args we can give as many inputs as we want, but keeping in mind that arguments must be non-keyworded arguments function that uses *agrs(*any_name_by_user) returns all the values in a tuple.
Example 2
def fun(*args): print(args) fun(1,2,3,4,5,'5.5', name = "rajesh")
When you run this code you got an error like this:
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-3e9b260b8436> in <module> 1 def fun(*args): 2 print(args) ----> 3 fun(1,2,3,4,5,'5.5', name = "rajesh") TypeError: fun() got an unexpected keyword argument 'name'
Here you pass key and value which is not supported by *args argument
Use case of *args
Let’s say we have a function that adds up the numbers passed as arguments but the number of arguments totally depends on the user.
How can we do that?
⇒ The answer is simply using *args in a function.
def add(*args): result = sum(args) print(result) add(1,2,3,4,5) add(1,2,3,4,5,6,7,8,9,10.5)
Output:
15 55.5
In the above function, at first, we pass five arguments, and the second time we pass ten arguments.
Here in this case function handles no matter how many numbers are passed as arguments and return sum as the result.
**kwargs in Python with examples
Using *agrs we are unable to give keyworded arguments what if we wanted to give keyworded arguments there where **kwargs comes into the picture.
def fun(**kwargs): print(kwargs) fun(name="shiv", age=22, faculty="computer")
{'name': 'shiv', 'age': 22, 'faculty': 'computer'}
Note: Function with **kwargs returns values as a dictionary.
To access the values inside the function
# to access the values def fun(**kwargs): for key, values in kwargs.items(): print(f"key: {key} , value: {values}") fun(name="shiv", age=22, faculty="computer")
Output:
key: name , value: shiv key: age , value: 22 key: faculty , value: computer
Use case of **kwargs
Let’s say we want to store the information of a student(name, id, age, address, phone number) using only one function. So, is it possible to do that? The answer is yes using **kwargs.
def student_info(**kwargs): print(kwargs) student_info(name="Shiv Shrestha",id = 10, age = 22, address="Nepal", ph_no = 9800000000 )
Output:
{'name': 'Shiv Shrestha', 'id': 10, 'age': 22, 'address': 'Nepal', 'ph_no': 9800000000 }
Mostly the use of *args and **kwargs is in the Python decorator.
Conclusion
In Python, concepts of *args and **kwargs are important for programmers. *args is used in a function that accepts non-keyworded arguments and **kwargs is used in a function whose task is to accept the keyworded arguments.
The first one returns the accepted values in the form of tuple and the latter one returns the accepted values in the form of a dictionary.
For more information follow this link
Happy Learning:-)