Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See <a href="https://docs.python.org/2.7/reference/compound_stmts.html#function-definitions" rel="noreferrer">Function Definitions</a> in the Language Reference.</p> <blockquote> <p>If the form <code>*identifier</code> is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form <code>**identifier</code> is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.</p> </blockquote> <p>Also, see <a href="https://docs.python.org/2.7/reference/expressions.html#calls" rel="noreferrer">Function Calls</a>.</p> <p>Assuming that one knows what positional and keyword arguments are, here are some examples:</p> <p>Example 1:</p> <pre><code># Excess keyword argument (python 2) example: def foo(a, b, c, **args): print "a = %s" % (a,) print "b = %s" % (b,) print "c = %s" % (c,) print args foo(a="testa", d="excess", c="testc", b="testb", k="another_excess") </code></pre> <p>As you can see in the above example, we only have parameters <code>a, b, c</code> in the signature of the <code>foo</code> function. Since <code>d</code> and <code>k</code> are not present, they are put into the args dictionary. The output of the program is:</p> <pre><code>a = testa b = testb c = testc {'k': 'another_excess', 'd': 'excess'} </code></pre> <p>Example 2:</p> <pre><code># Excess positional argument (python 2) example: def foo(a, b, c, *args): print "a = %s" % (a,) print "b = %s" % (b,) print "c = %s" % (c,) print args foo("testa", "testb", "testc", "excess", "another_excess") </code></pre> <p>Here, since we're testing positional arguments, the excess ones have to be on the end, and <code>*args</code> packs them into a tuple, so the output of this program is:</p> <pre><code>a = testa b = testb c = testc ('excess', 'another_excess') </code></pre> <p>You can also unpack a dictionary or a tuple into arguments of a function:</p> <pre><code>def foo(a,b,c,**args): print "a=%s" % (a,) print "b=%s" % (b,) print "c=%s" % (c,) print "args=%s" % (args,) argdict = dict(a="testa", b="testb", c="testc", excessarg="string") foo(**argdict) </code></pre> <p>Prints:</p> <pre><code>a=testa b=testb c=testc args={'excessarg': 'string'} </code></pre> <p>And</p> <pre><code>def foo(a,b,c,*args): print "a=%s" % (a,) print "b=%s" % (b,) print "c=%s" % (c,) print "args=%s" % (args,) argtuple = ("testa","testb","testc","excess") foo(*argtuple) </code></pre> <p>Prints:</p> <pre><code>a=testa b=testb c=testc args=('excess',) </code></pre>
    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.
 

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