Python Extended Keyword Arguments(*args, **kwargs)
Table Of Contents:
- What Are Extended Keyword Arguments?
- What Is Python *args?
- What Is Python **kwargs?
- Using Both *args and **kwargs.
(1) What Is Extended Keyword Arguments?
- Arguments are the values passed to the function, for further processing.
- Most of the time you will know how many arguments a function can take.
- But, sometimes the argument counts can vary, and then your function will fail to accommodate those arguments.
- To handle this issue we have ‘Extended Keyword Arguments‘.
Without Extended Keyword Arguments.
def student(name,rollno,subject):
print('Student Name:',name)
print('Student RollNo:',rollno)
print('Student Subject:',subject)
student('Abhispa','143','Math',99)
Output:
TypeError Traceback (most recent call last)
Input In [32], in <cell line: 1>()
----> 1 student('Abhispa','143','Math',99)
TypeError: student() takes 3 positional arguments but 4 were given
Note:
- Here, student( ) function takes the 3 arguments, which are ‘name’, ‘rollno’, and ‘subject’.
- But if you try to pass one more value to it like the mark, it will break.
With Extended Keyword Arguments.
def student(*args):
for value in args:
print(value)
student('Abhispa','143','Math',99)
Output:
Abhispa
143
Math
99
Note:
- Here, student( ) function can takes multiple arguments like, ‘name’, ‘rollno’, and ‘subject’.
- You can pass multiple arguments to it.
(2) What Is Python *args ?
- You can pass variable–length arguments by using the asterisk * symbol in front of a variable name.
- Here ‘arg’ is the variable name, that you can give anything.
- Finally, you need to iterate over the ‘args’ variable to get the individual values.
Example-1
def product(*args):
for value in args:
print(value)
product('Maggi','10 Rupees','205 Calories')
Output:
Maggi
10 Rupees
205 Calories
Note:
- Here, I can iterate over the ‘args’ argument like a list to get the individual values.
Example-2
def bike(*specs):
for value in specs:
print(value)
bike('Ninja 300','4Lakh','300 CC','25KMPL')
Output:
Ninja 300
4Lakh
300 CC
25KMPL
Note:
- Here, I can iterate over the ‘specs’ argument like a list to get the individual values.
(3) What Is Python Keyword Arguments **kwargs ?
- When we are using a single asterisk (*) we can pass values as an argument,
- These values can be anything like integers, strings, list, set, tuple or dictionaries.
- With the use of a double asterisk(**), you can provide a name to the variable as you pass it into the function.
- It is like passing a dictionary to the function.
Example-1
def students(**kwargs):
for key,value in kwargs.items():
print(f'Student {key}: {value}')
students(Name = 'Subrat', Mark = 78)
Output:
Student Name: Subrat
Student Mark: 78
Note:
- Here, I can iterate over the ‘kwargs’ argument like a dictionary to get the key and values.
Example-2
def country(**details):
for key,value in details.items():
print(f'Student {key}: {value}')
country(Name = 'India', Population = '1 Billion', Flag = 'Triranga')
Output:
Student Name: India
Student Population: 1 Billion
Student Flag: Triranga
Note:
- Here, I can iterate over the ‘details’ argument like a dictionary to get the key and values.
(4) Using Both *args and **kwargs ?
- *args receives arguments as an array
- **kwargs receives arguments as a dictionary.
Example-1
def employee(*args,**kwargs):
for i in args:
print(i)
for i , j in kwargs.items():
print(f'{i} : {j}')
employee('Praudyog','Learning Website',Name = 'Subrat',Degsignation= 'CEO')
Output:
Praudyog
Learning Website
Name : Subrat
Degsignation : CEO
Note:
- Here, the arguments which are only values will be stored inside ‘args’ variable.
- The arguments which are key and value pairs will be stored inside ‘kwargs’ variable.
Example-2
def laptops(*args, **kwargs):
for i in args:
print(i)
for i , j in kwargs.items():
print(f'{i} : {j}')
laptops('Dell',Model = 'Latitude 5420', Processor = 'Intel Core i5', Ram = '16GB')
Output:
Dell
Model : Latitude 5420
Processor : Intel Core i5
Ram : 16GB
Note:
- Here, the arguments which are only values will be stored inside ‘args’ variable.
- The arguments which are key and value pairs will be stored inside ‘kwargs’ variable.