python callable的理解
問題描述
class _IMP(object): def __init__(self, host=’127.0.0.1’, nport=8080, sport=8102):’’’ 8080 for nosecurity, 8443 for security, 8102 for nosecurity, 8103 for security’’’self.host = host # host address 127.0.0.1|10.6.18.3self.nport = nport # north port 8080|8443self.sport = sport # south port 8102|8103 def update(self, **kwargs):for k, v in kwargs.iteritems(): if hasattr(self, k):setattr(self, k, v) def as_dict(self):return {k: getattr(self, k) for k in dir(self) if not k.startswith(’_’) and not callable(getattr(self, k))}
a = _IMP()In [10]: a.as_dict()Out[10]: {’host’: ’127.0.0.2’, ’nport’: 8080, ’sport’: 8102}
as_dict 這里的 callable 是什么意思?
問題解答
回答1:callable用于檢查對(duì)象是否可調(diào)用,更簡(jiǎn)單一點(diǎn)來說就是判斷是不是方法
回答2:callable就是'可以被call的對(duì)象',可以被調(diào)用的對(duì)象.包括函數(shù),類,含有__call__方法的對(duì)象等.你可以在python repl中敲help(callable)看看,或者查python文檔,一般基本的問題和概念,都能解答了.
回答3:callable(Object)對(duì)象Object是否可被調(diào)用
相關(guān)文章:
1. PC 手機(jī)兼容的 編輯器2. html5和Flash對(duì)抗是什么情況?3. php如何獲取訪問者路由器的mac地址4. javascript - 在 vue里面用import引入js文件,結(jié)果為undefined5. python沒入門,請(qǐng)教一個(gè)問題6. 小程序怎么加外鏈,語(yǔ)句怎么寫!求救新手,開文檔沒發(fā)現(xiàn)7. git - 使用淘寶npm安裝hexo出現(xiàn)問題?8. javascript - vue-resource中如何設(shè)置全局的timeout?9. thinkPHP5中獲取數(shù)據(jù)庫(kù)數(shù)據(jù)后默認(rèn)選中下拉框的值,傳遞到后臺(tái)消失不見。有圖有代碼,希望有人幫忙10. 多選框?qū)戇M(jìn)數(shù)據(jù)庫(kù)怎么寫
