args-kwargs-python.
*args in Python:
In python, we can pass variable length of arguments(var-args) is function
To pass variable number of arguments we use *args
as parameter in functions.
By convention we use args
, you can use any valid identifier here.
It is used to pass a non-keyworded, variable-length argument list.
Type of *args
is tuple.
Example :
# function definition def myfun(*args): print(type(args)) check type for arg in args: print (arg) # function invocation print('first call') myfun('Android Apps', 'Java Prowess', 'C Prowess') print('second call') myfun(1,2,3,'Hello')OUTPUT
< class 'tuple'> it's tuple Android Apps Java Prowess C Prowess second call < class 'tuple'> 1 2 3 Hello
We can use *args
with extra arguments, but with extra care.
Example :
def myfun(args1, *args): print("args1 :", args1) for arg in args: print('var-args value:', arg) myfun('Android Apps', 'Java Prowess', 'C Prowess')OUTPUT
args1 : Android Apps var-args value: Java Prowess var-args value: C Prowess
As we can see first value is for args1 and rest of the values are for *args.
Now, fi we flip this arguments order then
def myfun(*args, args1):
print("args1 :", args1)
for arg in args:
print('var-args value:', arg)
myfun('Android Apps', 'Java Prowess', 'C Prowess') cause error
'''
the above method call will raise error,
because, every value will be accept by *args
and args1 will not get any value.
'''
OUTPUT
TypeError: myfun() missing 1 required keyword-only argument: 'args1'
So, in this case if you have *args
before any non-varargs then at the time of function calling,
you need to specify the value for non-varargs parameters.
def myfun(*args, args1): print("args1 :", args1) for arg in args: print('var-args value:', arg) myfun('Android Apps', 'Java Prowess', args1='C Prowess')OUTPUT
args1 : C Prowess var-args value: Android Apps var-args value: Java Prowess
In function, we can have only one *args
type argument.
**kwargs in Python :
In python function definition **kwargs
is used to pass keyworded, variable length of arguments.
Type of **kwargs
is dictionary.
def myfun(**kwargs): print(type(kwargs)) check type for k, v in kwargs.items(): print(f'Key: {k} and value :{v}') myfun(args1='Android Apps', args2='Java Prowess', args3='C Prowess')OUTPUT
< class 'dict'> its dictionary Key: args1 and value :Android Apps Key: args2 and value :Java Prowess Key: args3 and value :C Prowess
In function, we can have only one **kwargs
type argument.
Using *args
and **kwargs
in a function.
If you are using both in function as argument then make sure *args
is before the **kwargs
in function definition.
def myfun(*args, **kwargs): print('args:', args) print('Kwargs: ',kwargs) myfun("Android Apps", "ProwessApps", args1='Java Prowess', args2='C Prowess',)OUTPUT
args: ('Android Apps', 'ProwessApps') Kwargs: {'args1': 'Java Prowess', 'args2': 'C Prowess'}
Next chapter is function pointer in python
Online Live Training
We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.
Courses Offered :
- C Programming
- C++ Programming
- Data Structure
- Core Java
- Python
- Java Script
- Advance Java (J2EE)
- Hibernate
- Spring
- Spring Boot
- Data Science
- JUnit
- TestNg
- Git
- Maven
- Automation Testing - Selenium
- API Testing
NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.
If you have any requirements, please send them to prowessapps.in@gmail.com or info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in