introspection and functions

James Stroud jstroud at mbi.ucla.edu
Wed Aug 22 18:54:54 EDT 2007


yagyala wrote:
> Hi. I would like to be able to tell, at run time, how many parameters
> a function requires. Ideally I would like to be able to tell which are
> optional as well. I've tried looking at the functions attributes, but
> haven't found one that helps in this. How can I do this?
> 
> Thanks
> 

py> def doit(a, b, c, x=14):
...   pass
...
py> doit.func_code.co_argcount
4
py> doit.func_code.co_varnames
('a', 'b', 'c', 'x')
py> doit.func_defaults
(14,)

James



More information about the Python-list mailing list