__getattitem_ \__setattitem__\__delitem__

class Foo:
    def __getitem__(self, item):
        print('getitem',item)
        return self.__dict__[item]
    def __setitem__(self, key, value):
        print('setitem')
        self.__dict__[key]=value
    def __delitem__(self, key):
        print('delitem')
        self.__dict__.pop(key)
f1= Foo()
print(f1.__dict__)
f1['name']='egon'
f1['age']=19
print('=====>',f1.__dict__)

  

posted @ 2016-12-27 10:35  打不死的--蟑螂  阅读(178)  评论(0编辑  收藏  举报