getting argument names from a function

Neal Norwitz neal at metaslash.com
Tue Jul 10 17:10:18 EDT 2001


def func(a, b, c):
    x, y, z = 0, 1, 2
    numArgs = func.func_code.co_argcount
    print func.func_code.co_varnames[:numArgs]

Neal
--
dan wrote:
> 
> Actually, that grabs the entire namespace of the function, not just the
> function variables. I haven't found anything in the func_code tree that
> contains only the function argument keys...
> 
> thank you though,
> Dan
> 
> Fredrik Stenberg wrote:
> >
> > 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