鱼C论坛

 找回密码
 立即注册
查看: 1460|回复: 6

[已解决]第45讲课后题第二题 getattrbute重复出现46次的奇怪bug

[复制链接]
发表于 2019-8-14 11:11:51 | 显示全部楼层 |阅读模式
50鱼币
本帖最后由 yumjang 于 2019-8-16 21:29 编辑

问题如下:
在不上机验证的情况下,你能推断以下代码分别会显示什么吗??

  1. >>> class C:
  2.         def __getattr__(self, name):
  3.                 print(1)
  4.         def __getattribute__(self, name):
  5.                 print(2)
  6.         def __setattr__(self, name, value):
  7.                 print(3)
  8.         def __delattr__(self, name):
  9.                 print(4)           
  10. >>> c = C()
  11. >>> c.x = 1
  12. # 位置一,请问这里会显示什么?
  13. >>> print(c.x)
  14. # 位置二,请问这里会显示什么?
复制代码




答案是:
3
2
None


而我的程序跑出来,在None之后会多46个2,即便把小甲鱼的代码直接放进去跑,也是多46个执行__getattribute__的结果。非常的奇怪。

我单独尝试以下代码,输入c = C()后getattribute也会重复出现,虽然把c实例化逻辑上不应该调用getattribute....
  1. class C:
  2.         def __getattr__(self, name):
  3.                 print("getattr")
  4.         def __getattribute__(self, name):
  5.                 print("getattribute")
  6.                 return super().__getattribute__(name)
  7.         def __setattr__(self, name, value):
  8.                 print("setattr")
  9.                 return super().__setattr__(name, value)
  10.         def __delattr__(self, name):
  11.                 print("delattr")
复制代码


我把代码发给别人,没问题,但我自己这儿却总是出现这鬼现象。求大神告知为啥...
最佳答案
2019-8-14 11:11:52
emmm,首先代码并没有问题,其次你要看不同版本汇编器的特性,不同版本可能会出现兼容冲突
我偶尔会遇到

最佳答案

查看完整内容

emmm,首先代码并没有问题,其次你要看不同版本汇编器的特性,不同版本可能会出现兼容冲突 我偶尔会遇到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 11:11:52 | 显示全部楼层    本楼为最佳答案   
emmm,首先代码并没有问题,其次你要看不同版本汇编器的特性,不同版本可能会出现兼容冲突
我偶尔会遇到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 11:38:52 | 显示全部楼层
我是 Python 3.7.4,运行你的代码结果如下:
  1. setattr
  2. getattribute
  3. 1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 18:16:23 | 显示全部楼层
  1. Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license()" for more information.
  3. >>> class C:
  4.         def __getattr__(self, name):
  5.                 print(1)
  6.         def __getattribute__(self, name):
  7.                 print(2)
  8.         def __setattr__(self, name, value):
  9.                 print(3)
  10.         def __delattr__(self, name):
  11.                 print(4)

  12.                
  13. >>> c=C()
  14. >>> c.x=1
  15. 3
  16. >>> print(c.x)
  17. 2
  18. None
  19. >>> class C:
  20.         def __getattr__(self, name):
  21.                 print("getattr")
  22.         def __getattribute__(self, name):
  23.                 print("getattribute")
  24.                 return super().__getattribute__(name)
  25.         def __setattr__(self, name, value):
  26.                 print("setattr")
  27.                 return super().__setattr__(name, value)
  28.         def __delattr__(self, name):
  29.                 print("delattr")

  30.                
  31. >>> c=C()
  32. >>> c.x=1
  33. setattr
  34. >>> print(c.x)
  35. getattribute
  36. 1
复制代码
和楼上一样,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 18:45:08 | 显示全部楼层
但是很奇怪,我用的 Pycharm,直接在文件中编写代码运行结果是:
  1. setattr
  2. getattribute
  3. 1
复制代码

而在 Pycharm 的 Python Console 中编写代码运行会出现很多 getattribute,这就不知道是怎么回事了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-14 19:31:07 | 显示全部楼层
zltzlt 发表于 2019-8-14 18:45
但是很奇怪,我用的 Pycharm,直接在文件中编写代码运行结果是:

而在 Pycharm 的 Python Console 中编 ...

不很知道,我在IDLE中的运行结果始终都是
  1. setattr
  2. getattribute
  3. 1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-15 10:03:06 | 显示全部楼层
zltzlt 发表于 2019-8-14 18:45
但是很奇怪,我用的 Pycharm,直接在文件中编写代码运行结果是:

而在 Pycharm 的 Python Console 中编 ...

Python Console 使用 IPython,可能是解释器问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 09:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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