*args and **kwargs in python
*args -The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list.
**kwargs -The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).
def args_funct(*args_anything): # *args used for multiple inputs without specifying multiple parameters
print("## ARGS MESSAGES")
for i in args_anything:
print(i)
def kwargs_funct(**kwargs_anything): # **kwargs used for passing keywords and value without specifying multiple parameters
print("## KWARGS MESSAGES")
for key,value in kwargs_anything.items():
print(f"{key} ka {value}")
list1=["radhe","radhe","shyam","shyam"]
dictionary1={"radhe":"shyam","sita":"ram"}
args_funct(*list1)
kwargs_funct(**dictionary1)
Comments
Post a Comment