美文网首页
python 字典类实战

python 字典类实战

作者: 高峥 | 来源:发表于2020-04-16 12:34 被阅读0次
    class ResponseBody(dict):
    
        def __init__(self, response, body=None):
            body_data = body or {}
            self.update(body_data)
            self.response = response
            self.body = body_data
    
        def __str__(self):
            body = super(ResponseBody, self).__str__()
            return "Response: %s\nBody: %s" % (self.response, body)
    
    class ResponseBodyData(object):
       
        def __init__(self, response, data):
            self.response = response
            self.data = data
    
        def __str__(self):
            return "response: %s\nBody: %s" % (self.response, self.data)
    
    class ResponseBodyList(list):
    
        def __init__(self, response, body=None):
            body_data = body or []
            self.extend(body_data)
            self.response = response
    
        def __str__(self):
            self.body = super(ResponseBodyList, self).__str__()
            return "response: %s\nBody: %s" % (self.response, self.body)
    
    res = ResponseBodyData({},d)
    res_d = ResponseBody({},d)
    print res.data
    print res_d.body
    print res_d.body.items()
    print res_d.items()
    
    
    '''
    {'a': {'v': {'c': 'e'}, 'one': 'one'}, 'b': {'c': 'c'}}
    {'a': {'v': {'c': 'e'}, 'one': 'one'}, 'b': {'c': 'c'}}
    [('a', {'v': {'c': 'e'}, 'one': 'one'}), ('b', {'c': 'c'})]
    [('a', {'v': {'c': 'e'}, 'one': 'one'}), ('b', {'c': 'c'})]
    '''
    

    相关文章

      网友评论

          本文标题:python 字典类实战

          本文链接:https://www.haomeiwen.com/subject/hpkrvhtx.html