callable() 函數用於檢查一個對象是否是可調用的。如果返回True,object仍然可能調用失敗;但如果返回False,調用對象ojbect絕對不會成功。
對於函數, 方法, lambda 函式, 類, 以及實現了 __call__ 方法的類實例, 它都返回 True。
def test(func): # 判斷func如果是函數,就執行他,如果不是函數,直接返回 # 判斷func是否可調用,如果可以調用,就是true if callable(func): ret = func else: ret = func return ret # print(test(123)) def test2(): return '111' print(test(test2()))