I am no longer writing on this blog, go over to my new blog here. - Hrishikesh [1st Jan 2016]

A Personal Blog on Stuffs I Love.

Thursday, 23 April 2015

*args and **kwargs in Python Explained Super Easy

Thursday, April 23, 2015 Posted by Hrishikesh , , , , 6 comments
Using variable length argument lists in python is accomplished by using *args and **kwargs, New python programmers often get confused on this particular topic, but it's quite easy once you know what it is really.  By the way, you must know tuples and dictionary to understand the concept clearly.

First of all, it's about the asterisks(the stars), 
A single * for non-keyword arguments (eg. *args)
Double ** for key-worded arguments  (eg. **kwargs)
For some function run
Non-key-worded arguments: run(3,4)
Key-worded arguments: run(cool=2,boy=7)  
and not the names i.e args or kwargs, so it can be *dragon and  **hobbit or anything you want it to be. The python programmers like to keep it like what it is(i.e args & kwargs).
 
We'll first dig into *args and the **kwargs and then see both of them together. But first let's look at an example.The Output is shown at the end of the article.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/python
def myfunction(hola, *args, **kwargs):
        print "The formal argument is :", hola
        print args #prints *args as the tuple
        for i in args:
            print "Elements in args is : ", i
        print kwargs #prints **kwargs as the dictionary
        for i in kwargs:
            print "Elements in kwargs is: ", kwargs[i]

#Calling the Function myfunction()
myfunction(12,45,56,7,567,356,234,24,pokemon=200,ninja=100)

Now, we use args and kwargs in function definitions to have variable number of arguments. 

Let's talk *(single star) i.e *args now,

The *(single star) is for non-keyword arguments and gets passed into a tuple. And after the first formal argument (which is hola in our example) all other non-keyworded arguments get passed into a tuple named args or whatever you name it. 

Let's talk **(double star) i.e *kwargs now,

The **(double star) is for keyword arguments and gets passed into a dictionary. Any keyworded argument will be a member of dictionary named kwargs or whatever you name it.

Now, Look at the output, 

The formal argument is : 12
(45, 56, 7, 567, 356, 234, 24)
Elements in args is :  45
Elements in args is :  56
Elements in args is :  7
Elements in args is :  567
Elements in args is :  356
Elements in args is :  234
Elements in args is :  24
{'ninja': 100, 'pokemon': 200}
Elements in kwargs is:  100
Elements in kwargs is:  200


Now, You must be able to figure out the mystery, I hope I am able to clear your doubts, If you have any questions you may drop them in.  

6 comments:

  1. Replies
    1. Typing mistake Louis . "named" . Thanks. I edited it.

      Delete
  2. elaborate on some real world uses and advamtages in future..

    ReplyDelete
    Replies
    1. Yes Sure Vinayak. I am writing full tutorials and making videos for creating quality webapps(not shitty ones), once I complete. i'll publish. :) thanks a lot!

      Delete