2011-01-12 104 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',)) 
>>>