introspection and functions

Scott David Daniels daniels at dsl-only.net
Thu Aug 23 05:53:23 CEST 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
> 
This really only will work for those functions that are simply
constructed.  You are better off not trying to do this, or code
like the following will confound your code:

     from functools import partial

     def somefunction(a=23, b=14, c='hi'):
         print 'Finally'

     f = partial(somefunction, b=13)
     g = partial(f, a=19)
     h = partial(g, c=123)
     print whatargs(h)



More information about the Python-list mailing list