search

Home  >  Q&A  >  body text

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.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 是什么意思?

巴扎黑巴扎黑2785 days ago719

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:30:55

    callable is used to check whether the object is callable. To put it more simply, it is to determine whether it is a method

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:30:55

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:30:55

    callable(Object) Whether the object Object can be called

    reply
    0
  • Cancelreply