在python函数中获取参数名称列表 [英] Getting list of parameter names inside python function
本文介绍了在python函数中获取参数名称列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在python中获取方法参数名称
Possible Duplicate:
Getting method parameter names in python
是否有一种简单的方法可以在python函数中获取参数名称列表?
Is there an easy way to be inside a python function and get a list of the parameter names?
例如:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
谢谢
推荐答案
好吧,我们实际上并不需要inspect
.
Well we don't actually need inspect
here.
>>> 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__
.
For Python 2.5 and older, use func_code
instead of __code__
, and func_defaults
instead of __defaults__
.
这篇关于在python函数中获取参数名称列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文