Key-value pair as variable length argument

Under certain circumstances, there might be a situation, where the key-value pair needs to be passed as the variable length argument to the function. Let's take one example here. In the given example, the key-value pairs are passed as variable length arguments: 

def infocity(**var):
print var
for key, value in var.items():
print "%s == %s" %(key,value)


infocity(name="l4w", age = 20, city="Los Angeles")
infocity(name="John",age=45, city="London", sex="male", medals=0)

Using a for loop we print out each key-value pair that is passed to the function. On the first call, we pass only few key-value pairs, while in the second call to the function, we pass a bit more key-value pairs and we get the output as shown here:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68