python callable的理解
問(wèn)題描述
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 是什么意思?
問(wèn)題解答
回答1:callable用于檢查對(duì)象是否可調(diào)用,更簡(jiǎn)單一點(diǎn)來(lái)說(shuō)就是判斷是不是方法
回答2:callable就是'可以被call的對(duì)象',可以被調(diào)用的對(duì)象.包括函數(shù),類,含有__call__方法的對(duì)象等.你可以在python repl中敲help(callable)看看,或者查python文檔,一般基本的問(wèn)題和概念,都能解答了.
回答3:callable(Object)對(duì)象Object是否可被調(diào)用
相關(guān)文章:
1. python - oslo_config2. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問(wèn)題3. 實(shí)現(xiàn)bing搜索工具urlAPI提交4. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)5. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。6. windows誤人子弟啊7. 冒昧問(wèn)一下,我這php代碼哪里出錯(cuò)了???8. 如何用筆記本上的apache做微信開(kāi)發(fā)的服務(wù)器9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. mysql優(yōu)化 - MySQL如何為配置表建立索引?
