getting argument names from a function

Fredrik Stenberg mail at fredriks.org
Tue Jul 10 18:41:40 CEST 2001


On Tue, 10 Jul 2001, dan wrote:

> def aFunction(arg1, arg2):
>    print arg1, arg2
>
> is there a way I can get to the arguments of aFunction (i.e. 'arg1' and
> 'arg2')?
>
> I've tried dir(..) on the function object and subsequent object and can't
> find what I'm looking for. Is it possible to do what I want?
>
> thanks,
> Dan

If the function has a func_code object you can try this approach,
I have never use it myself and there probably is better ways.


Use Ctrl-D (i.e. EOF) to exit.
>>> def foo(bar,bar2):
...     print "foobar"
...
>>> foo.func_code.co_varnames
('bar', 'bar2')



I think this will only work for functions written in python, and not
extensions written in C (atleast thats what my brain thinks).

/fredriks




More information about the Python-list mailing list