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用于檢查對象是否可調(diào)用,更簡單一點(diǎn)來說就是判斷是不是方法
回答2:callable就是'可以被call的對象',可以被調(diào)用的對象.包括函數(shù),類,含有__call__方法的對象等.你可以在python repl中敲help(callable)看看,或者查python文檔,一般基本的問題和概念,都能解答了.
回答3:callable(Object)對象Object是否可被調(diào)用
相關(guān)文章:
1. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????2. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項3. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫4. mysql取模分表與分表5. gvim - 誰有vim里CSS的Indent文件, 能縮進(jìn)@media里面的6. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號?正則如何寫?7. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯誤8. PHP類屬性聲明?9. objective-c - ios 怎么實(shí)現(xiàn)微信聯(lián)系列表 最好是swift10. java - 安卓接入微信登錄,onCreate不會執(zhí)行
