Getting all arguments and values passed to a function

Python

Python Problem Overview


I have a Python function, fetch_data, that goes and hits a remote API, grabs some data, and returns it wrapped in a response object. It looks a bit like the below:

def fetch_data(self, foo, bar, baz, **kwargs):
    response = Response()
    # Do various things, get some data
    return response

Now, it's possible that the response data says "I have more data, call me with an incremented page parameter to get more". Thus, I'd essentially like to store "The Method Call" (function, parameters) in the response object, so I can then have a Response.get_more() which looks at the stored function and parameters, and calls the function again with (almost) the same parameters, returning a new Response

Now if fetch_data were defined as fetch_data(*args, **kwargs) I could just store (fetch_data, args, kwargs) in response. However I have self, foo, bar and baz to worry about - I could just store (fetch_data, foo, bar, baz, kwargs) but that's a highly undesirable amount of repetition.

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.

Python Solutions


Solution 1 - Python

> 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 - Python

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 - Python

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 - Python

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 :-))

Solution 5 - Python

inspect.getargspec is deprecated since version 3.0. Use signature() and Signature Object, which provide a better introspecting API for callables.

>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
...     pass

>>> sig = signature(foo)

>>> str(sig)
'(a, *, b:int, **kwargs)'

>>> str(sig.parameters['b'])
'b:int'

>>> sig.parameters['b'].annotation
<class 'int'>

Solution 6 - Python

I believe method of choice is getcallargs from inspect as it returns real arguments with which function will be invoked. You pass a function and args and kwargs to it (inspect.getcallargs(func, *args, **kwds)), it will return real method's arguments used for invocation, taking into consideration default values and other stuff. Have a look at an example below.

from inspect import getcallargs

# we have a function with such signature
def show_params(first, second, third=3):
    pass

# if you wanted to invoke it with such params (you could get them from a decorator as example)
args = [1, 2, 5]
kwargs = {}
print(getcallargs(show_params, *args, **kwargs))
#{'first': 1, 'second': 2, 'third': 5}

# here we didn't specify value for d
args = [1, 2, 3, 4]
kwargs = {}

# ----------------------------------------------------------
# but d has default value =7
def show_params1(first, *second, d = 7):
    pass


print(getcallargs(show_params1, *args, **kwargs))
# it will consider b to be equal to default value 7 as it is in real method invocation
# {'first': 1, 'second': (2, 3, 4), 'd': 7}

# ----------------------------------------------------------
args = [1]
kwargs = {"d": 4}

def show_params2(first, d=3):
    pass


print(getcallargs(show_params2, *args, **kwargs))
#{'first': 1, 'd': 4}

https://docs.python.org/3/library/inspect.html

Solution 7 - Python

import inspect

def f(x, y):
    print(
        inspect.getargvalues(inspect.currentframe())
    )

f(1, 2)

Result:
ArgInfo(args=['x', 'y'], varargs=None, keywords=None, locals={'y': 2, 'x': 1})

Solution 8 - Python

kwargs won't have 'foo', 'bar' or 'bad' as keys, so you can add those entries (w/ their values) to kwargs and just store (fetch_data, kwargs).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKristian GlassView Question on Stackoverflow
Solution 1 - PythonkuzzoorooView Answer on Stackoverflow
Solution 2 - PythonMartijn PietersView Answer on Stackoverflow
Solution 3 - PythonJDGView Answer on Stackoverflow
Solution 4 - PythonNPEView Answer on Stackoverflow
Solution 5 - PythonGerman LashevichView Answer on Stackoverflow
Solution 6 - PythonAlexView Answer on Stackoverflow
Solution 7 - PythonVitaly ZdanevichView Answer on Stackoverflow
Solution 8 - PythonScott HunterView Answer on Stackoverflow