Categories
Uncategorized

The **kwargs Function Argument – Python

**kwargs can be used for multiple arguments which don’t really have to be specified. All the information is then stored as a dictionary.

So if we have a function like this

def myFunction(**kwargs):
    print type(kwargs)

and we run it like this

myFunction(a1='one',a2='two',a3='three')

we get this

<type 'dict'>

confirming it is a dictionary.

We could also call each argument with a loop

def myFunction(**kwargs):
    for str_key in kwargs:
        print str_key, kwargs[str_key]

If we run it with the above arguments, we should get

a1 one
a3 three
a2 two

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s