How to query a function and get a list of expected parameters?

George Sakkis george.sakkis at gmail.com
Fri Sep 29 15:15:47 EDT 2006


Matthew Wilson wrote:

> I'm writing a function that accepts a function as an argument, and I
> want to know to all the parameters that this function expects.  How can
> I find this out in my program, not by reading the source?
>
> For example, I would want to know for the function below that I have to
> pass in two things:
>
> def f(x1, x2): return x1 * x2
>
> It would be nice to get the names also.
>
> All help is welcome.
>
> TIA
>
> Matt

Check out the inspect module
(http://www.python.org/doc/current/lib/inspect-classes-functions.html):
>>> from inspect import getargspec
>>> getargspec(f)
(['x1', 'x2'], None, None, None)

George




More information about the Python-list mailing list