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用于检查对象是否可调用,更简单一点来说就是判断是不是方法
回答2:callable就是'可以被call的对象',可以被调用的对象.包括函数,类,含有__call__方法的对象等.你可以在python repl中敲help(callable)看看,或者查python文档,一般基本的问题和概念,都能解答了.
回答3:callable(Object)对象Object是否可被调用
相关文章:
1. MySQL客户端吃掉了SQL注解?2. 网页爬虫 - python爬虫翻页问题,请问各位大神我这段代码怎样翻页,还有价格要登陆后才能看到,应该怎么解决3. javascript - 图片能在网站显示,但控制台仍旧报错403 (Forbidden)4. mysql - AttributeError: ’module’ object has no attribute ’MatchType’5. 数据库 - MySQL 单表500W+数据,查询超时,如何优化呢?6. android - Windows系统下运行react-native App时,报下面的错误?7. php自学从哪里开始?8. 求大神帮我看看是哪里写错了 感谢细心解答9. phpstady在win10上运行10. objective-c - iOS怎么实现像QQ或者微信的实时推送
