__setattr__和__delattr__和__getattr__
__setattr__, __delattr__, __getattr__(重要)
点出来拦截器方法
1.如果去对象中取属性,一旦取不到, 就会进入到__getattr
2, 如果去对象中赋值属性, 会进入到setattr
3, 如果删除对象中的属性, 会进入__delattr
class Foo:
x = 1
def __init__(self, y):
self.y = y
def __getattr__(self, item):
print('----> from getattr:你找的属性不存在')
def __setattr__(self, key, value):
print('----> from setattr')
# self.key = value # 这就无限递归了,一直是自己调用自己
# self.__dict__[key] = value # 应该使用它
def __delattr__(self, item):
print('----> from delattr')
# del self.item # 无限递归了 一直是自己调用自己
self.__dict__.pop(item)
f1 = Foo(10)
一 、__setattr__
- 添加/修改属性会触发它的执行
print(f1.__dict__
) # 因为你重写了__setattr__,凡是赋值操作都会触发它的运行,你啥都没写,就是根本没赋值,除非你直接操作属性字典,否则永远无法赋值
f1.z = 3
print(f1.__dict__)
二、__delattr__
- 删除属性的时候会触发
f1.__dict__['a'] = 3 # 我们可以直接修改属性字典,来完成添加/修改属性的操作
del f1.a
print(f1.__dict__)
----> from delattr
{}
三、__getattr__
- 只有在使用点调用属性且属性不存在的时候才会触发
f1.xxxxxx
----> from getattr:你找的属性不存在
四、字典通过点的方式取值和赋值
# 写一个类继承字典, 实现通过 . 的形式取值,可以通过括号取值
class Mydict(dict):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def __getattr__(selfs, item):
print("进入__getattr__")
# print(item)
return selfs[item]
def __setattr__(self, key, value):
self[key] = value
print("进入__setattr__")
# print('*' * 50)
# dic = Mydict(name='randy', age='18')
dic = Mydict()
dic.name = 'laowang'
#
# # 获取name值
print(dic['name'])
print("获取name值", dic.name)
# # 添加值
print("添加值**" * 10)
dic.sex = 'male'
print(dic['sex'])
dic['sex'] = 'male'
print(dic['sex'])
五、总结
- __setattr__:添加/修改(赋值)属性会触发它的执行,注意会出现死递归,通过,self.__dict__[key] = value
- __delattr__:删除属性的时候会触发,注意会出现死递归,通过, self.__dict__.pop(item)
- __getattr__:只有在使用点调用属性且属性不存在的时候才会触发