Python Tutorial

Showing posts with label Keyword arguments. Show all posts
Showing posts with label Keyword arguments. Show all posts

Monday, February 18, 2013

Keyword arguments

For keyword arguments we need to use two asterisks (**) before the argument name.


All source code available on github

def addValue(**kwargs):
    total = 0
    for key, value in kwargs.items():
        print "Key",key,"value",value
        total += value
    return total


print "Total",addValue(item1=2, item2=3 ,item3=5, item4=7)

Output:
Key item2 value 3
Key item3 value 5
Key item1 value 2
Key item4 value 7
17