Getting all arguments and values passed to a function

Solution 1:

Essentially, I'm trying to work out how to, from within a function, get a completely populated *args and **kwargs, including the function's named parameters.

How about saving the arguments via locals() at the beginning of the function?

def my_func(a, *args, **kwargs):
    saved_args = locals()
    print("saved_args is", saved_args)
    local_var = 10
    print("saved_args is", saved_args)
    print("But locals() is now", locals())

my_func(20, 30, 40, 50, kwarg1='spam', kwarg2='eggs')

It gives this output:

saved_args is {'a': 20, 'args': (30, 40, 50), 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}
saved_args is {'a': 20, 'args': (30, 40, 50), 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}
But locals is now {'a': 20, 'saved_args': {...}, 'args': (30, 40, 50), 'local_var': 10, 'kwargs': {'kwarg1': u'spam', 'kwarg2': u'eggs'}}

Hat tip: https://stackoverflow.com/a/3137022/2829764

Solution 2:

Not something I'd do, but you could use inspect.signature to introspect the arguments your method takes:

>>> import inspect
>>> def foobar(foo, bar, baz):
...     return inspect.signature(foobar)
... 
>>> foobar(1, 2, 3)
<Signature (foo, bar, baz)>

The returned Signature instance has an ordered collection of parameters (the .parameters attribute) which can then be used together with locals() to produce a list of your argument values:

>>> def foobar(foo, bar, baz):
...     sig, foobar_locals = inspect.signature(foobar), locals()
...     return [foobar_locals[param.name] for param in sig.parameters.values()]
...
>>> foobar(1, 2, 3)
[1, 2, 3]

However, you really only need such magic when doing advanced function decorators and the like. I think it's overkill here.

Solution 3:

I am not sure this is exactly what you want, but locals() provides a dictionary of local variables.

>>> def foo(bar, toto):
...     print(locals())
...
>>> foo(3,'sometext')
{'toto': 'sometext', 'bar': 3}

Solution 4:

I think a more Pythonic way is to turn your function into a generator, fetching and yielding data for as long as the server keeps returning stuff.

This should result in neat code and would enable you to side-step all of the complexities of preserving the arguments across iterations (Python will magically do it for you :-))