# Python program to illustrate
# **kwargs for variable number of keyword arguments
def info(**kwargs):
for key, value in kwargs.items():
print ("%s == %s" %(key, value))
# Driver code
info(first ='This', mid ='is', last='Me')
#1. When used as part of a function definition
def f(self, *args, **kwargs):
#it is used to signify an arbitrary number of positional or
#keyword arguments, respectively.
#The point to remember is that inside the function args will be a tuple,
#and kwargs will be a dict.
#2. When used as part of a function call,
args = (1, 2)
kwargs = {'last': 'Doe', 'first': 'John'}
self.f(*args, **kwargs)
#the * and ** act as unpacking operators.
#args must be an iterable, and kwargs must be dict-like.
#The items in args will be unpacked and sent to the function
#as positional arguments, and the key/value pairs in kwargs
#will be sent to the function as keyword arguments.
#Thus,
self.f(*args, **kwargs)
#is equivalent to
self.f(1, 2, last='Doe', first='John')
Using **kwargs to pass the variable keyword arguments to the function
# Welcome to softhunt.net
def intro(**data):
print("
Data type of argument:",type(data))
for key, value in data.items():
print("{} is {}".format(key,value))
intro(Firstname="Ranjeet", Lastname="Kumar", Age=22, Phone=1122334455)
intro(Firstname="Softhunt", Lastname=".net", Email="softhunt.net@gmail.com", Country="Pakistan", Age=22, Phone=1122334455)
def myFun(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)
# Now we can use *args or **kwargs to
# pass arguments to this function :
args = ("Geeks", "for", "Geeks")
myFun(*args)
kwargs = {"arg1": "Geeks", "arg2": "for", "arg3": "Geeks"}
myFun(**kwargs)