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__)
f['x'] = 123123123123
print(f.__dict__)
f['x']

2、删除 

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

  

相关文章:

  • 2021-05-19
  • 2021-09-11
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2021-06-05
  • 2022-02-28
  • 2021-06-15
猜你喜欢
  • 2022-12-23
  • 2021-05-20
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
相关资源
相似解决方案