471,831 Members | 777 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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

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
Sep 29 '06 #1
2 1195
Matt,

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

For more see the inspect module.

-Nick Vatamaniuc

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

--
A better way of running series of SAS programs:
http://overlook.homelinux.net/wilson...asAndMakefiles
Sep 29 '06 #2
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/li...nctions.html):
>>from inspect import getargspec
getargspec(f)
(['x1', 'x2'], None, None, None)

George

Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Yisroel Markov | last post: by
4 posts views Thread by paii, Ron | last post: by
10 posts views Thread by Fredrik Tolf | last post: by
36 posts views Thread by Liam.M | last post: by
4 posts views Thread by Tony Lownds | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.