How do I ask a function for the names of its parameters in Python? [duplicate]
Possible Duplicate:
Getting list of parameters inside python function
E.g. supposes I have
def foo(a, b='B'): return
How can开发者_开发百科 I ask foo to tell me that it has required parameter 'a', and parameter 'b', which has 'B' as it's default value?
Use inspect.getargspec
.
def foo(a, b='B'): pass
import inspect
print inspect.getargspec(foo)
It may appear to be unclear which argument the default is for, but since non-default arguments can't follow default arguments, the default has to be for the 2nd argument.
Edit: The linked duplicate is good, an answer there shows you can get the same info without inspect
, using func.func_code.co_varnames
and func.func_defaults
or func.__defaults__
.
精彩评论