Is there a way to get back from a return value from inspect.getcallargs(func) to a *args, **kw pair that can actually be used to call the func?
Use case: say I am writing a decorator, and I want to change a particular argument of a function by name. Here’s the beginning of some code to do this:
@fix_x
def a(x):
print x
@fix_x
def b(**q):
print q['x']
def fix_x(func):
def wrapper(*args, **kw):
argspec = inspect.getargspec(func)
callargs = inspect.getcallargs(func, *args, **kw)
if 'x' in callargs:
callargs['x'] += 5
elif 'x' in callargs[argspec.keywords]:
callargs[argspec.keywords]['x'] += 5
# ...and now I'd like a simple way to call func with callargs...?
(I’m actually doing something more elaborate with the callargs between building them and wanting to make a call with them, but this should give an idea of what I’m looking for.)
No, there isn’t currently a good way to do this, however it is in the works for Python 3.3!
See PEP 362 — Function Signature Object for how this new feature will work.