可能重複:
How to find out the arity of a method in PythonPython函數參數的長度?
例如,我已經聲明瞭一個函數:
def sum(a,b,c):
return a + b + c
我想要得到的 「求和」 函數的參數長度。
somethig是這樣的:some_function(sum)返回3
它如何在Python中完成?
更新:
因爲我想寫接受另一個函數作爲參數和參數傳遞給它的功能我問這個問題。
def funct(anotherFunct, **args):
,我需要驗證:
if(len(args) != anotherFuct.func_code.co_argcount):
return "error"
你可以看到源代碼,對不對?這顯然是3.你還需要知道什麼? – 2010-10-12 11:18:14
爲什麼不嘗試傳遞'* args'並返回'return len(args)'或'return len(filter(None,args))'!!! – shahjapan 2010-10-12 11:36:35
,因爲我想寫一個接受另一個函數作爲參數和參數傳遞它的函數。 def function(anotherFunct,** args):我需要驗證:if(len(args)!= anotherFuct.func_code.co_argcount):return「error」 – Zango 2010-10-12 13:22:51