标签:sof name family IV exception size str find 进入
__getattr__
查找不到类属性的时候调用
class BeiMenChuiXue:
def __init__(self, name):
self.name = name
def __getattr__(self, item):
print("%s not find", item)
raise Exception("实例方法没有找到")
if __name__ == ‘__main__‘:
bei_men_chui_xue = BeiMenChuiXue("北门吹雪")
print(bei_men_chui_xue.name)
# 没有这个属性
print(bei_men_chui_xue.hello)
__getattribute__
无条件进入,无论是查找属性是否存在,能不重写则不去重写
class BeiMenChuiXue:
def __init__(self, name):
self.name = name
def __getattribute__(self, item):
print("%s find" % item)
if __name__ == ‘__main__‘:
bei_men_chui_xue = BeiMenChuiXue("北门吹雪")
# 能找到,会发现其实是__getattribute__的返回值
print(bei_men_chui_xue.name)
# 没有这个属性
print(bei_men_chui_xue.hello)
Python-类属性查询协议-__getattr__ __getattribute__
标签:sof name family IV exception size str find 进入
原文地址:https://www.cnblogs.com/2bjiujiu/p/9142543.html