1.getattribute
class Naither():
def __init__(self):
self.name = "老干妈"
self.age = 60
def __getattribute__(self, item):
if item == "name":
res = super().__getattribute__(item)
return res[0]+"*"+res[-1]
else:
return super().__getattribute__(item)
laoganma = Naither()
print(laoganma.name)
print(laoganma.age)
2.getattr
class Sunther():
def __init__(self):
self.name = "孙贼,别跑!"
self.age = 100
def __getattr__(self, item):
if "name" in item:
return self.name
elif "age" in item:
return self.age
sunquan = Sunther()
print(sunquan.names)
print(sunquan.ages)
3.setattr
class S_Nvther():
def __setattr__(self, key, value):
if key == "name":
pass
else:
super().__setattr__(key, value)
luoli = S_Nvther()
luoli.sex = "孙女乖乖"
print(luoli.sex)
print(luoli.__dict__)
4.delattr
class MySelf():
def __init__(self):
self.name = "帅气的文广哥哥,长腿欧巴,颜值男神"
self.girlfriend = "刘亦菲"
def __delattr__(self, item):
if item == "name":
pass
else:
object.__delattr__(self, item)
wenguang = MySelf()
print(wenguang.__dict__)
del wenguang.girlfriend
print(wenguang.__dict__)