编程语言
首页 > 编程语言> > Python9-hashilib模块-day28(大年初三)

Python9-hashilib模块-day28(大年初三)

作者:互联网

__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__)

 

标签:__,.__,hobby,self,hashilib,dict,大年初三,print,Python9
来源: https://www.cnblogs.com/zhangtengccie/p/10355597.html