鱼C论坛

 找回密码
 立即注册
查看: 2341|回复: 5

[已解决]关于魔法方法__getattr__的疑问

[复制链接]
发表于 2020-3-2 16:39:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jiujiaomutou 于 2020-3-2 16:42 编辑

class C:
   
        def __getattribute__(self, name):
            print('getattribute')
            return super().__getattribute__(name)


        def __setattr__(self, name, value):
            print('setattr')
            super().__setattr__(name, value)


        def __delattr__(self, name):
            print('delattr')
            super().__delattr__(name)
            
        def __getattr__(self, name):
            print('getattr')
            super().__getattr__(name)


c = C()
c.x




如果载入如上命令行,会打印:
AttributeError                            Traceback (most recent call last)<ipython-input-12-b710c0919212> in <module>()     18      19 c = C()---> 20 c.x<ipython-input-12-b710c0919212> in __getattr__(self, name)     15        def __getattr__(self, name):     16            print('getattr')---> 17            super().__getattr__(name)     18      19 c = C()AttributeError: 'super' object has no attribute '__getattr__'但是如果我定义一个类B:      class B:
        def __getattr__(self,name):
            print(1)

    class C(B):

        def __getattribute__(self, name):
            print('getattribute')
            return super().__getattribute__(name)

        def __setattr__(self, name, value):
            print('setattr')
            super().__setattr__(name, value)

        def __delattr__(self, name):
            print('delattr')
            super().__delattr__(name)

        def __getattr__(self, name):
            print('getattr')
            super().__getattr__(name)

c = C()
c.x

会打印:

getattribute

getattr

1

不会有报错

关于这两个对比的疑问是,是不是由于__getattr__魔法方法本身比较特殊,区别与__getattribute__等魔法方法,没有默认的父对象?还是由于啥原因导致,这两个命令的行的打印结果不同?烦请大神解答一下,谢谢


最佳答案
2020-3-2 16:56:03
知道在没有继承父类的时候,使用spuer的MRO搜寻路径吗?    class->object    会直接从object搜寻是不是存在这个方法,很明显,object.__dict__里面不存在__getattr__的方法。

https://fishc.com.cn/thread-157472-1-1.html    Supper函数
小Q截图-20200302163128.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-2 16:41:27 | 显示全部楼层
错误信息中说了,父类没有 __getattr__() 方法,所以不要写 super().__getattr__() 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-2 16:44:35 | 显示全部楼层
zltzlt 发表于 2020-3-2 16:41
错误信息中说了,父类没有 __getattr__() 方法,所以不要写 super().__getattr__() 。

__getattribute__
__setattr__
是默认有父类的?毕竟这个命令行里面用了。

就是__getattr___比较特殊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 16:56:03 | 显示全部楼层    本楼为最佳答案   
知道在没有继承父类的时候,使用spuer的MRO搜寻路径吗?    class->object    会直接从object搜寻是不是存在这个方法,很明显,object.__dict__里面不存在__getattr__的方法。

https://fishc.com.cn/thread-157472-1-1.html    Supper函数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 16:57:17 | 显示全部楼层
jiujiaomutou 发表于 2020-3-2 16:44
__getattribute__
__setattr__
是默认有父类的?毕竟这个命令行里面用了。

谈不上 “特殊”,父类(object)就是没有 __getattr__ 方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 16:59:32 | 显示全部楼层
  1. >>> object().__getattr__('x')
  2. Traceback (most recent call last):
  3.   File "<pyshell#0>", line 1, in <module>
  4.     object().__getattr__('x')
  5. AttributeError: 'object' object has no attribute '__getattr__'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-29 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表