Introduction To Python *args and **kwargs For Beginners – Part 2

In the first part I explained to you guys the purpose of *args in python programming language through some simple practical examples. In this second part I will give you some other examples but with *kwargs magic variables. Same as *args the *kwargs python magic variables are used in function definitions but the difference is that they allow the developer to pass keyworded arguments to the function.

Take the following example.

def display_stuff(**kwargs):
    if kwargs is not None:
        print kwargs

Now in the python interpreter run the python function display_stuff as shown below.

display_stuff(name='jetbird', passion='development', language='python')

What do you see? Do you see the following printed on your screen?

{'passion': 'development', 'name': 'jetbird','language': 'python'}

As you can see **kwargs help the developer to give arbitrary keyworded arguments to a function and access them later in a dictionary. For example I can easily print the value of key ‘passion’ by using the following code.

def display_stuff(**kwargs):
    if kwargs is not None:
        print kwargs['passion']

Now run the function display_stuff as shown below.

display_stuff(name='jetbird', passion='development', language='python')

I get the following when run the above piece of code.

development

But what happens if the key ‘passion’ does not exist in our dictionary kwargs. We will get a KeyError, thats what is going to happen. To make sure we are right run the function display_stuff like shown below.

display_stuff(error='KeyError')

I get the following when I run the piece of python code shown above.

/home/oltjano/Desktop/<ipython-input-37-8bdf8d5bedd2> in display_stuff(**kwargs)
 1 def display_stuff(**kwargs):
 2     if kwargs is not None:
 ---->     3 print kwargs['passion']
 4
KeyError: 'passion'

So it is better for us to follow a different approach when writing the function display_stuff. And one thing we should check for is if the key we want to access is in the kwargs dictionary.

So in your python shell, type the following piece of code.

def display_stuff(**kwargs):
if kwargs is not None and 'passion' in kwargs:
print kwargs['passion']

Writing function display_stuff as shown above makes sure Python does not throw a KeyError when we try to print the value of key which is not available in the dictionary. And since I like to show things to you geeks run the following.

display_stuff(name='jetbird')

Did you get any error? Of course no, unless you did not change the way function display_stuff works. You can also print every key and value that is in the dictionary by doing the following.

def display_stuff(**kwargs):
    if kwargs is not None:
        for key, value in kwargs.items():
            print key, '==>', value

And run the following to see what kind of output is going to be printed on your python shell.

display_stuff(name='jetbird', passion='developer')

I get the following output.

passion ==> developer
 name ==> jetbird

**kwargs are very useful when it comes to proxy functions in python. And to understand it better run the following python code in the python shell.

def myfunc(name='jetbird', likes='python'):
print name, likes

Now in the same shell define a proxy function.

def proxyfunc(**kwargs):
myfunc(**kwargs)

Now run the function proxyfunc like shown below.

proxyfunc(name='test', likes='test')

If done right you will get the following output.

test test

I think is enough for this second part. I hope to have cleared any confusion regarding *args and **kwargs magic variables in python or at least helped to get you started. I am also thinking about another part where I give real word examples of *args and **kwargs. Until then keep practising python and do not forget to share this tutorial with others.