Issue 3135: inspect.getcallargs() - Python tracker (original) (raw)
I'd like to propose a new function for inclusion to the inspect module -- getcallargs(func, *args, **kwds) -- that returns a dict which maps the formal arguments of a function (or other callable) to the values passed as args and kwds, just as Python has to do when calling func(*args, **kwds). For example:
def func(a, b='foo', c=None, *x, **y): ... pass sorted(getcallargs(func, 5, z=3, b=2).items()) [('a', 5), ('b', 2), ('c', None), ('x', ()), ('y', {'z': 3})]
This is handy when writing decorators, or more generally when one would want to do some minimal type checking without actually calling the function.
I have posted a recipe at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/551779; I can clean it up and submit a proper patch if it's deemed useful enough for the stdlib.