Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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" rel="noreferrer">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" rel="noreferrer">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/" rel="noreferrer">Extended Iterable Unpacking</a>), though it gives a list instead of a tuple in this context:</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/" rel="noreferrer">PEP 3102</a>):</p> <pre><code>def func(arg1, arg2, arg3, *, kwarg1, kwarg2): pass </code></pre> <p>Such function accepts only 3 positional arguments, and everything after <code>*</code> can only be passed as keyword arguments.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload