Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all arguments and values passed to a function

Tags:

python

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.

like image 351
Kristian Glass Avatar asked May 23 '12 16:05

Kristian Glass


People also ask

How do you pass all arguments to a function?

Within any function, you can use the arguments variable to get an array-like list of all of the arguments passed into the function. You don't need to define it ahead of time. It's a native JavaScript object. You can access specific arguments by calling their index.

Which keyword is used to access the array of arguments of a function?

javascript function with array parameter.

What is it called when you pass a function as an argument?

Higher Order Functions Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.


2 Answers

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

like image 151
kuzzooroo Avatar answered Nov 04 '22 05:11

kuzzooroo


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.

like image 29
Martijn Pieters Avatar answered Nov 04 '22 05:11

Martijn Pieters