Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using Python >= 2.7 <code>inspect.getcallargs()</code> does this for you out of the box. You'd just pass it the decorated function as the first argument, and then the rest of the arguments exactly as you plan to call it. Example:</p> <pre><code>&gt;&gt;&gt; def f(p1, p2, k1=None, k2=None, **kwargs): ... pass &gt;&gt;&gt; from inspect import getcallargs </code></pre> <p>I'm planning to do <code>f('p1', 'p2', 'p3', k2='k2', extra='kx1')</code> (note that k1 is being passed positionally as p3), so...</p> <pre><code>&gt;&gt;&gt; call_args = getcallargs(f, 'p1', 'p2', 'p3', k2='k2', extra='kx1') &gt;&gt;&gt; call_args {'p2': 'p2', 'k2': 'k2', 'k1': 'p3', 'p1': 'p1', 'kwargs': {'extra': 'kx1'}} </code></pre> <p>If you know the decorated function won't use <code>**kwargs</code>, then that key won't appear in the dict, and you're done (and I'm assuming there's no <code>*args</code>, since that would break the requirement that everything have a name). If you <em>do</em> have <code>**kwargs</code>, as I have in this example, and want to include them with the rest of the named arguments, it takes one more line:</p> <pre><code>&gt;&gt;&gt; call_args.update(call_args.pop('kwargs')) &gt;&gt;&gt; call_args {'p2': 'p2', 'k2': 'k2', 'k1': 'p3', 'p1': 'p1', 'extra': 'kx1'} </code></pre> <p>Update: for Python >= 3.3, see <a href="https://docs.python.org/3/library/inspect.html#inspect.Signature.bind" rel="nofollow noreferrer"><code>inspect.Signature.bind()</code></a> and the related <a href="https://docs.python.org/3/library/inspect.html#inspect.signature" rel="nofollow noreferrer"><code>inspect.signature</code> function</a> for functionality similar to (but more robust than) <code>inspect.getcallargs()</code>.</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. This table or related slice is empty.
    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