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