python - Retrieve the names of (and default values of) the arguments accepted by a specified function -


say have function, foo has arguments...some positional , keyword:

def foo(a, b=1.0, c=none):     print     print c     return 2 * b 

is there function can call foo argument (or method of foo can call) return me list of positional arguments of foo list of tuples of keyword arguments of foo along default values? specifically, calling:

the_function_i_want(foo) 

should return

(('a',), (('b', 1.0), ('c', none))) 

or that. clear not want way figure out values passed foo 1 particular time called. instead want information function signature of foo in programatic way.

(the use case have in mind automatically make web form able submit appropriate data serve arguments of specified function. if call web_form(foo) in appropriate way, able render web form spaces each of arguments of foo default calling values pre-filled in appropriate way.)

i think you're describing inspect.getargspec:

>>> inspect import getargspec >>> getargspec(foo) argspec(args=['a', 'b', 'c'], varargs=none, keywords=none, defaults=(1.0, none)) 

you can use signature in 3.x:

>>> inspect import signature >>> signature(foo) <signature (a, b=1.0, c=none)> 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -