python內置函數之callable

callable(object):

這個函數的作用是檢查一個對象是否可以調用的。如果返回True,object仍然可能調用失敗。但是返回False,則調用object一定不會成功。


需要注意的是:類是可以調用的,類的調用返回這個類的一個實例。而類的實例需要定義了__call__()纔可以調用。


例子:

class example:
    def __call__(self):
        print 'called'

test = example() #produce an instance of a class
test()  #the output is called
callable(example)  #the output is True
callable(test)  #the output is True, if not define __call__ function, the output is False


發佈了42 篇原創文章 · 獲贊 185 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章