关于gettribute魔法方法的使用与疑惑
根据鱼哥的视频讲解,还是对__getattribute__这个魔法方法有点不了解{:10_258:} ,只是大致明白简单的用法根据以下代码提出疑问
>>> class C:
def __getattribute__(self,name):
print('getattribute')
return super().__getattribute__(name)
def __getattr__(self,name):
print('getattr')
def __setattr__(self,name,value):
print('setattr')
super().__setattr__(name,value)
def __delattr__(self,name):
print('delattr')
super().__delattr__(name)
>>> c = C()
>>> c.x
getattribute
getattr
>>> c.x = 1
setattr
>>> c.x
getattribute
1
>>> del c.x
delattr
代码中的:
def __getattribute__(self,name):
print('getattribute')
return super().__getattribute__(name)
__getattribute__魔法方法是在调用属性的时候执行的,但是调用属性执行后打印了'getattribute',但是return返回的内容在去找到object的__getattribute__魔法方法参数为name,这里是干什么用的呢{:10_258:}
然后我自己写了一段代码,执行起来不对劲,参考如下:
>>> class C:
def __getattribute__(self,name):
print('getattribute')
def __getattr__(self,name):
print('getattr')
>>> c = C()
>>> c.x
getattribute
为什么访问不存在的属性__getattr__魔法方法没有执行呢{:10_258:} 在线等大佬{:10_285:}
因为 __getattribute__ 魔法方法会在你访问属性时候自动调用,而当找不到这个属性时候就会引发报错
但是如果有设置 __getattr__ 魔法方法那么这个魔法方法就会捕获这个错误,返回 __getattr__ 设置的返回值
而你的代码重写了 __getattribute__魔法方法,但是没有调用父类的__getattribute__导致失去了原有的功能
而且你重写的 __getattribute__魔法方法,也只有 print('getattribute') 这一个功能,其他毫无作用了,所以就不会引发报错,也不会导致触发 __getattr__ 了
UncleMonster 发表于 2020-8-2 16:50
在线等大佬
设置了 return super().__getattribute__(name) 就能调用父类的 __getattribute__ 方法,正常触发 __getattr__ 魔法方法了:
class C:
def __getattribute__(self, name):
print('getattribute')
return super().__getattribute__(name)
def __getattr__(self, name):
print('getattr')
c = C()
c.x
输出结果:
getattribute
getattr
Twilight6 发表于 2020-8-2 16:52
因为 __getattribute__ 魔法方法会在你访问属性时候自动调用,而当找不到这个属性时候就会引发报错
...
意思就是如果设置return去找到实例化对象的__getattribute__(name)魔法方法的参数的返回值,如果出现报错就会被后面的__getattr__捕获是吗{:10_258:} UncleMonster 发表于 2020-8-2 16:57
意思就是如果设置return去找到实例化对象的__getattribute__(name)魔法方法的参数的返回值,如果出现报错 ...
简单来说这里就是为了继承原有的 __getattribute__ 魔法方法功能,因为你重写时候会覆盖之前默认的功能
你只是想添加个调用时候打印效果来看执行先后顺序,所以要保留原有的功能就要调用父类的方法
你如果没有设置__getattribute__ 魔法方法,当你访问一个不存在的属性时候就会直接触发 __getattr__ 魔法方法 Twilight6 发表于 2020-8-2 17:01
简单来说这里就是为了继承原有的 __getattribute__ 魔法方法功能,因为你重写时候会覆盖之前默认的功 ...
感谢大佬{:10_266:}顺便问一下个性签名怎么搞{:10_250:} UncleMonster 发表于 2020-8-2 17:02
感谢大佬顺便问一下个性签名怎么搞
用 [ img ]图片链接 [ / img ] 这样的代码就好,里面空格去了 这里是防止被编辑器吃了哈~ Twilight6 发表于 2020-8-2 17:04
用 [ img ]图片链接 [ / img ] 这样的代码就好,里面空格去了 这里是防止被编辑器吃了哈~
OK,不会,谢谢{:10_285:}
页:
[1]