2011-01-12 102 views
2

可能重复:
How to find out the arity of a method in PythonPython函数参数计算

给出一个Python函数,我该如何编程方式确定的需要的参数是多少?

+2

可能的重复:http://stackoverflow.com/questions/990016/how-to-find-out-the-arity-of-a-method-in-python,http://stackoverflow.com/questions/ 3913963 /长度的论点-的-蟒功能/ 3915056#3915056 – mouad 2011-01-12 22:52:26

回答

10

检查是你在这种情况下

>>> 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',)) 
>>>