callable(object)
中文說明:檢查物件object是否可呼叫。如果返回True,object仍然可能呼叫失敗;但如果返回False,呼叫物件ojbect絕對不會成功。
注意:類是可呼叫的,而類的例項實現了__call__()方法才可呼叫。
版本:該函式在python2.x版本中都可用。但是在python3.0版本中被移除,而在python3.2以後版本中被重新新增。
英文說明:Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a __call__() method.
程式碼例項:
>>> callable(0) False >>> callable("mystring") False >>> def add(a, b): … return a + b … >>> callable(add) True >>> class A: … def method(self): … return 0 … >>> callable(A) True >>> a = A() >>> callable(a) False >>> class B: … def __call__(self): … return 0 … >>> callable(B) True >>> b = B() >>> callable(b) True