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.3
self.nport = nport # north port 8080|8443
self.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
是什么意思?
callable is used to check whether the object is callable. To put it more simply, it is to determine whether it is a method
A callable is an "object that can be called", an object that can be called. It includes functions, classes, objects with __call__ methods, etc.
You can type help (callable) in the python repl to see it, or check the python documentation , all basic questions and concepts can be answered.
callable(Object) Whether the object Object can be called