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.