What does ** (double star) and * (star) do for Python parameters?
<p>In the following method definitions, what does the <code>*</code> and <code>**</code> do for <code>param2</code>?</p>

<pre><code>def foo(param1, *param2):
def bar(param1, **param2):
</code></pre>

<p>The <code>*args</code> and <code>**kwargs</code> is a common idiom to allow arbitrary number of arguments to functions as described in the section <a href="http://docs.python.org/dev/tutorial/controlflow.html#more-on-defining-functions">more on defining functions</a> in the Python documentation.</p>

<p>The <code>*args</code> will give you all function parameters <a href="https://docs.python.org/dev/tutorial/controlflow.html#arbitrary-argument-lists">as a tuple</a>:</p>

<pre><code>In [1]: def foo(*args):
   ...:     for a in args:
   ...:         print a
   ...:         
   ...:         

In [2]: foo(1)
1


In [4]: foo(1,2,3)
1
2
3
</code></pre>

<p>The <code>**kwargs</code> will give you all 
<strong>keyword arguments</strong> except for those corresponding to a formal parameter as a dictionary.</p>

<pre><code>In [5]: def bar(**kwargs):
   ...:     for a in kwargs:
   ...:         print a, kwargs[a]
   ...:         
   ...:         

In [6]: bar(name='one', age=27)
age 27
name one
</code></pre>

<p>Both idioms can be mixed with normal arguments to allow a set of fixed and some variable arguments:</p>

<pre><code>def foo(kind, *args, **kwargs):
   pass
</code></pre>

<p>Another usage of the <code>*l</code> idiom is to <strong>unpack argument lists</strong> when calling a function.</p>

<pre><code>In [9]: def foo(bar, lee):
   ...:     print bar, lee
   ...:     
   ...:     

In [10]: l = [1,2]

In [11]: foo(*l)
1 2
</code></pre>

<p>In Python 3 it is possible to use <code>*l</code> on the left side of an assignment (<a href="http://www.python.org/dev/peps/pep-3132/">Extended Iterable Unpacking</a>):</p>

<pre><code>first, *rest = [1,2,3,4]
first, *l, last = [1,2,3,4]
</code></pre>

<p>Also Python 3 adds new semantic (refer <a href="https://www.python.org/dev/peps/pep-3102/">PEP 3102</a>):</p>

<pre><code>def func(arg1, arg2, arg3='default', *, kwarg1='abc', kwarg2='xyz'):
    pass
</code></pre>

<p>Such function accepts only 2 positional arguments, and everything after <code>*</code> can only be passed as keyword argument, not positional one.</p>

<p>In Python 2 similar was true for all parameters after <code>*args</code>.</p>
