Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The catch-all <code>*args</code> parameter <em>must</em> come after any explicit arguments:</p> <pre><code>def foo(func, named_arg=None, *args): </code></pre> <p>If you also add the catch-all <code>**kw</code> keywords parameter to a definition, then that <em>has</em> to come after the <code>*args</code> parameter:</p> <pre><code>def foo(func, named_arg=None, *args, **kw): </code></pre> <p>Mixing explicit keyword arguments and the catch-all <code>*args</code> argument does lead to unexpected behaviour; you cannot both use arbitrary positional arguments <em>and</em> explicitly name the keyword arguments you listed at the same time.</p> <p>Any extra positionals beyond <code>func</code> are first used for <code>named_arg</code> which can also act as a positional argument:</p> <pre><code>&gt;&gt;&gt; def foo(func, named_arg = None, *args): ... print func, named_arg, args ... &gt;&gt;&gt; foo(1, 2) 1 2 () &gt;&gt;&gt; foo(1, named_arg=2) 1 2 () &gt;&gt;&gt; foo(1, 3, named_arg=2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: foo() got multiple values for keyword argument 'named_arg' &gt;&gt;&gt; foo(1, 2, 3) 1 2 (3,) </code></pre> <p>This is because the any second positional argument to <code>foo()</code> will <em>always</em> be used for <code>named_arg</code>.</p> <p>In Python 3, the <code>*args</code> parameter <em>can</em> be placed before the keyword arguments, but that has a <em>new</em> meaning. Normally, keyword parameters can be specified in the call signature as positional arguments (e.g. call your function as <code>foo(somefunc, 'argument')</code> would have <code>'argument'</code> assigned to <code>named_arg</code>). By placing <code>*args</code> or a plain <code>*</code> in between the positional and the named arguments you exclude the named arguments from being used as positionals; calling <code>foo(somefunc, 'argument')</code> would raise an exception instead.</p>
    singulars
    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.
 

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