Reputation: 38510
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.
Upvotes: 127
Views: 203328
Reputation: 7408
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()} # Updated to make a copy per loco.loop
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
Upvotes: 183
Reputation: 4413
I believe the method to choose 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#inspect.getcallargs
Upvotes: 12
Reputation: 41
I think the easiest/cleanest (...ughhh "pythonic") way would be to just use a decorator function... no need to use "inspect" or iterate over locals() -- just grab them directly from the decorator function. Try something like:
def call_info(func: callable):
def wrapper(*args, **kwargs):
print(f'Calling {func} with args: {args}, kwargs: {kwargs}')
return func(*args, **kwargs)
return wrapper
@call_info
def test_func(name, *args, size: int = 2, cheese: bool = True, **kwargs):
print('nothing to see here...')
print(f'but if you wanted to know.... name={name}, args={args}, size=size}, cheese={cheese}, kwargs={kwargs}')
test_func(44)
test_func(name='hello', size=4, cheese=False)
would print something like this:
Calling <function test_func at 0x0000015614079E40> with args: (44,), kwargs: {}
nothing to see here...
but if you wanted to know.... name=44, args=(), size=2, cheese=True, kwargs={}
Calling <function test_func at 0x0000015614079E40> with args: (), kwargs: {'name': 'hello', 'size': 4, 'cheese': False}
nothing to see here...
but if you wanted to know.... name=hello, args=(), size=4, cheese=False, kwargs={}
You can add the @call_info decorator to any methods/functions you wish to view the passed variables. Do whatever you like to the variables, before/after the main function call... just modify the wrapper.
there's a pretty good link describing how to use decorators here:
https://realpython.com/primer-on-python-decorators/
also described in the python documentation:
https://docs.python.domainunion.de/3/glossary.html#term-decorator
Upvotes: 1
Reputation: 379
>>> def fun(a,b,c):
... print(locals())
...
>>> fun(1,2,3)
{'a': 1, 'b': 2, 'c': 3}
Upvotes: 0
Reputation: 563
Try this:
def my_func(a, *args, **kwargs):
v = locals()
allargs = [v[i] for i in my_func.__code__.co_varnames if i in v and i not in ('args','kwargs')]
allargs.extend(args)
return allargs, kwargs
List allargs combines required and optional arguments. The code has to be placed at the beginning of function, at least v = locals().
Upvotes: 0
Reputation: 1121484
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.
Upvotes: 58
Reputation: 14812
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})
Upvotes: 8
Reputation: 506
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}
Upvotes: 32
Reputation: 2433
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'>
Upvotes: 12
Reputation: 500257
I think a more Pythonic way is to turn your function into a generator, fetching and yield
ing 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 :-))
Upvotes: 16
Reputation: 49803
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).
Upvotes: 1