parsing function parameters

Peter Otten __peter__ at web.de
Wed Aug 3 12:51:27 EDT 2011


Lee Harr wrote:

> I am trying to get some information about a function
> before (and without) calling it.

If you allow for the function arguments to be evaluated it's easy (requires 
Python 2.7):

>>> import inspect
>>> def get_args(*args, **kw):
...     return args, kw
...
>>> argstr = "1, 2, z=42"
>>> def f(a, b, x=1, y=2, z=3):
...     pass
...
>>> args, kw = eval("get_args(%s)" % argstr)
>>> inspect.getcallargs(f, *args, **kw)
{'a': 1, 'x': 1, 'b': 2, 'z': 42, 'y': 2}





More information about the Python-list mailing list