__getitem__\__setitem__\__delitem__
class Foo: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def __getitem__(self, item): if hasattr(self,item): return self.__dict__[item] def __setitem__(self,key,value): self.__dict__[key] = value def __delitem__(self, key): del self.__dict__[key] f = Foo('egon',38,'男') print(f['name']) f['hobby'] = '男' print(f.hobby,f['hobby']) print(f.__dict__) del f['hobby'] print(f.__dict__)