Python function parameter count [duplicate]
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.
精彩评论