鱼C论坛

 找回密码
 立即注册
查看: 782|回复: 9

[已解决]请教图中的A被打印两次的原因

[复制链接]
发表于 2018-7-25 18:37:42 | 显示全部楼层 |阅读模式
30鱼币
        题目如图,我也调试过了,调试中发生的事情是这样的B达到return a.__getattribute__(name)后去到了A的__getattribute__(self,name),这时A的那句会被打印第一次,然后A的__getattribute__(self,name)执行完后又到B的__getattribute__(self,name),然后B又跳到A,这时A的那句会被打印第二次,这次A执行完后程序就结束了,想不通第一次A为什么会跳回B。
最佳答案
2018-7-25 18:37:43
用如下代码证明第一个应该是由super().__getattribute__(name)调用的
  1. class A:
  2.     x = 1
  3.     def __getattr__(self,name):
  4.         print("A的__getattr__")
  5.     def __getattribute__(self,name,isB = False):
  6.         if(isB):
  7.             print("B调用")
  8.         else:
  9.             print("非B调用")
  10.         print("A的__getattribute__")
  11.         return super().__getattribute__(name)
  12. class B(A):
  13.     x = 2
  14.     def __getattr__(self,name):
  15.         print("B的__getattr__")
  16.     def __getattribute__(self,name):
  17.         print("B的__getattribute__")
  18.         a = A()
  19.         return a.__getattribute__(name,True)
  20. b = B()
  21. print(b.x)
复制代码

结果:
B的__getattribute__
非B调用
A的__getattribute__
B调用
A的__getattribute__
1
无标题.png
无标题2.png

最佳答案

查看完整内容

用如下代码证明第一个应该是由super().__getattribute__(name)调用的 结果: B的__getattribute__ 非B调用 A的__getattribute__ B调用 A的__getattribute__ 1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-25 18:37:43 | 显示全部楼层    本楼为最佳答案   
用如下代码证明第一个应该是由super().__getattribute__(name)调用的
  1. class A:
  2.     x = 1
  3.     def __getattr__(self,name):
  4.         print("A的__getattr__")
  5.     def __getattribute__(self,name,isB = False):
  6.         if(isB):
  7.             print("B调用")
  8.         else:
  9.             print("非B调用")
  10.         print("A的__getattribute__")
  11.         return super().__getattribute__(name)
  12. class B(A):
  13.     x = 2
  14.     def __getattr__(self,name):
  15.         print("B的__getattr__")
  16.     def __getattribute__(self,name):
  17.         print("B的__getattribute__")
  18.         a = A()
  19.         return a.__getattribute__(name,True)
  20. b = B()
  21. print(b.x)
复制代码

结果:
B的__getattribute__
非B调用
A的__getattribute__
B调用
A的__getattribute__
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-25 19:18:37 | 显示全部楼层
2018725_191516.png 2018725_191805.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-25 20:47:52 | 显示全部楼层
Python.爱好者 发表于 2018-7-25 19:38
用如下代码证明第一个应该是由super().__getattribute__(name)调用的

结果:

大神,能再回答一下为什么
  1. class B(A):
  2.         x = 2
  3.         def __getattr__(self,name):
  4.                 print("B的__getattr__")
  5.         def __getattribute__(self,name):
  6.                 print("B的__getattribute__")
  7.                 return object().__getattribute__(name)

  8.        
  9. b = B()
  10. b.x
复制代码

输出:
B的__getattribute__
B的__getattr__吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-25 21:07:20 | 显示全部楼层

大神,能再回答一下为什么:
  1. class B(A):
  2.         x = 2
  3.         def __getattr__(self,name):
  4.                 print("B的__getattr__")
  5.         def __getattribute__(self,name):
  6.                 print("B的__getattribute__")
  7.                 return object().__getattribute__(name)

  8.         
  9. b = B()
  10. b.x
复制代码

输出为:
B的__getattribute__
B的__getattr__吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-25 21:21:59 | 显示全部楼层
当类的属性被调用时,会自动调用魔法方法__getattritute__
魔法方法__getattr__应该是调用对象不存在的属性时才调用。
你的输出结果调用了__getattr__方法,如果最后一行的object()改成super()就正常了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-25 23:16:26 | 显示全部楼层
这不是回答的很清楚明白了。你有认真看?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 08:11:43 | 显示全部楼层
ba21 发表于 2018-7-25 23:16
这不是回答的很清楚明白了。你有认真看?

我后面的代码你有认真看?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-26 08:12:26 | 显示全部楼层
Python.爱好者 发表于 2018-7-25 19:38
用如下代码证明第一个应该是由super().__getattribute__(name)调用的

结果:

谢谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-27 09:01:53 | 显示全部楼层
修改一下
  1. class A:
  2.     x = 1
  3.     def __getattr__(self,name):
  4.         print("A的__getattr__")
  5.     def __getattribute__(self,name):
  6.         print(name)
  7.         print("A的__getattribute__")
  8.         return super().__getattribute__(name)
  9. class B(A):
  10.     x = 2
  11.     def __getattr__(self,name):
  12.         print("B的__getattr__")
  13.     def __getattribute__(self,name):
  14.         print("B的__getattribute__")
  15.         a = A()
  16.         return a.__getattribute__(name)
  17. b = B()
  18. print(b.x)
复制代码

输出
B的__getattribute__
__getattribute__
A的__getattribute__
x
A的__getattribute__
1
你的第16行调用了a.__getattruibute__获取a.__getattruibute__再运行,所以运行了两次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 19:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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