开发者

How do I ask a function for the names of its parameters in Python? [duplicate]

This question already has answers here: Closed 11 years ago.

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__.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜