Possible Duplicate:
Getting method parameter names in python
有没有一种简单的方法可以在python函数中获取参数名列表?
例如:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
谢谢
Possible Duplicate:
Getting method parameter names in python
有没有一种简单的方法可以在python函数中获取参数名列表?
例如:
def func(a,b,c):
print magic_that_does_what_I_want()
>>> func()
['a','b','c']
谢谢
其实我们不需要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__
.