开发者

Python function parameter count [duplicate]

开发者 https://www.devze.com 2023-02-04 15:51 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: How to find out the arity of a method in Python
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

How to find out the arity of a method in Python

Given a python function, how do I programmatically determine the number of parameter开发者_如何学运维s it takes?


inspect is your friend in this case

>>> def testFunc(arg1, arg2, arg3, additional='test'):
...     print arg1
... 
>>> import inspect
>>> inspect.getargspec(testFunc)
ArgSpec(args=['arg1', 'arg2', 'arg3', 'additional'], varargs=None, keywords=None, defaults=('test',))
>>> 


From outside the function, you can use inspect.getargspec(): http://docs.python.org/library/inspect.html#inspect.getargspec


Take a look at the inspect.getargspec(func) command. That gives you a tuple, the first element of which is a list of the required parameters.

0

精彩评论

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