Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Matthew Wilson
Guest
 
Posts: n/a
#1: Sep 29 '06
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



--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles

Nick Vatamaniuc
Guest
 
Posts: n/a
#2: Sep 29 '06

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


Matt,

In [26]: inspect.getargspec(f)
Out[26]: (['x1', 'x2'], None, None, None)

For more see the inspect module.

-Nick Vatamaniuc

Matthew Wilson wrote:
Quote:
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
>
>
>
--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
George Sakkis
Guest
 
Posts: n/a
#3: Sep 29 '06

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


Matthew Wilson wrote:
Quote:
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/li...nctions.html):
Quote:
Quote:
Quote:
>>from inspect import getargspec
>>getargspec(f)
(['x1', 'x2'], None, None, None)

George

Closed Thread