day7_python之面向对象item系列(__getitem__,__setitem__,__delitem__)

class Foo:
    def __getitem__(self, item):
        print('=====>get')
        return self.__dict__[item]

    def __setitem__(self, key, value):
        self.__dict__[key] = value
        # setattr(self,key,value)

    def __delitem__(self, key):
        self.__dict__.pop(key)


f = Foo()
f.x = 1
print(f.x)
print(f.__dict__)

1、增加

f['x'] = 123123123123
print(f.__dict__)
f['x']

2、删除 

del f['x']
print(f['x'])

  

posted @ 2017-11-25 16:21  xiechao  阅读(2687)  评论(1编辑  收藏  举报
levels of contents