getting argument names from a function

dan dan at eevolved.com
Tue Jul 10 14:47:08 EDT 2001


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