鱼C论坛

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

[已解决]第45讲课后题测试题第二题

[复制链接]
发表于 2020-2-28 19:51:07 | 显示全部楼层 |阅读模式

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

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

x
  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.                
  11. >>> c = C()
  12. >>> c.x = 1
  13. # 位置一,请问这里会显示什么?
  14. >>> print(c.x)
  15. # 位置二,请问这里会显示什么?
复制代码


想问一下为什么print(c.x)不显示1呢,不是给赋值为1了吗
最佳答案
2020-2-28 19:57:32
素手就琴 发表于 2020-2-28 19:55
那调用完为啥也不显示呢

因为你在调用 __getattribute__ 时只会打印 2 而不做其他事。

或者你可以这样(调用父类的方法):

  1. >>> class C:
  2.         def __getattr__(self, name):
  3.                 print(1)
  4.                 return super().__getattr__(name)
  5.         def __getattribute__(self, name):
  6.                 print(2)
  7.                 return super().__getattribute__(name)
  8.         def __setattr__(self, name, value):
  9.                 print(3)
  10.                 return super().__setattr__(name, value)
  11.         def __delattr__(self, name):
  12.                 print(4)
  13.                 return super().__delattr__(name)

  14.         
  15. >>> c = C()
  16. >>> c.x = 1
  17. 3
  18. >>> print(c.x)
  19. 2
  20. 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-28 19:53:28 | 显示全部楼层
访问属性会先调用 __getattribute__ 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-28 19:55:33 | 显示全部楼层
zltzlt 发表于 2020-2-28 19:53
访问属性会先调用 __getattribute__ 。

那调用完为啥也不显示呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:57:17 | 显示全部楼层
Snipaste_2020-02-28_19-53-40.jpg

__setattr__是指属性被赋值时执行的函数,所以你给c.x赋值1的时候,打印了3

__getattribute__ 是获取属性值时执行的函数,
所以print(c.x),先打印了2 -> 这是由于获取c.x执行了__getattribute__打印出来的2,
之后还要执行print(),由于__getattribute__没有返回任何值,所以print打印出来None
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 19:57:32 | 显示全部楼层    本楼为最佳答案   
素手就琴 发表于 2020-2-28 19:55
那调用完为啥也不显示呢

因为你在调用 __getattribute__ 时只会打印 2 而不做其他事。

或者你可以这样(调用父类的方法):

  1. >>> class C:
  2.         def __getattr__(self, name):
  3.                 print(1)
  4.                 return super().__getattr__(name)
  5.         def __getattribute__(self, name):
  6.                 print(2)
  7.                 return super().__getattribute__(name)
  8.         def __setattr__(self, name, value):
  9.                 print(3)
  10.                 return super().__setattr__(name, value)
  11.         def __delattr__(self, name):
  12.                 print(4)
  13.                 return super().__delattr__(name)

  14.         
  15. >>> c = C()
  16. >>> c.x = 1
  17. 3
  18. >>> print(c.x)
  19. 2
  20. 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-28 19:59:38 | 显示全部楼层
zltzlt 发表于 2020-2-28 19:57
因为你在调用 __getattribute__ 时只会打印 2 而不做其他事。

或者你可以这样(调用父类的方法):

哦哦,是本来应该显示1的,但是重写了这个函数就不能显示了是吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-28 20:00:17 | 显示全部楼层
素手就琴 发表于 2020-2-28 19:59
哦哦,是本来应该显示1的,但是重写了这个函数就不能显示了是吧

是的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-1 19:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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