#!/usr/bin/env python
# -- coding = 'utf-8' --
# Author Allen Lee
# Python Version 3.5.1
# OS Windows 7
#有序化字典
class Mydict(dict):

    def __init__(self):
        self.li = []
        super(Mydict,self).__init__()

    def __setitem__(self, key, value):
        self.li.append(key)
        super(Mydict,self).__setitem__(key,value)

    def __str__(self):
        temp_list = []
        for key in self.li:
            value = self.get(key)
            temp_list.append("'%s':%s" % (key,value,))
        temp_str = "{" + ",".join(temp_list) + "}"
        return temp_str

obj = Mydict()
obj['k1'] = 123
obj['k2'] = 424
print(obj)
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐