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.
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.
javascript function with array parameter.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With