在python函数中获取参数名称的列表
发布于 2021-02-02 23:10:16
有没有一种简单的方法可以进入python函数并获取参数名称列表?
例如:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
谢谢
关注者
0
被浏览
95
1 个回答
-
好吧,我们实际上不需要
inspect
这里。>>> func = lambda x, y: (x, y) >>> >>> func.__code__.co_argcount 2 >>> func.__code__.co_varnames ('x', 'y') >>> >>> def func2(x,y=3): ... print(func2.__code__.co_varnames) ... pass # Other things ... >>> func2(3,3) ('x', 'y') >>> >>> func2.__defaults__ (3,)
对于Python 2.5及更高版本,请使用
func_code
代替__code__
和func_defaults
代替__defaults__
。