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

 

上一篇:js里的一些逻辑运算


下一篇:Python9-反射-day27(大年初三)