鱼C论坛

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

[已解决]【46讲课后题】描述符太tm难了!!!

[复制链接]
发表于 2020-3-8 14:48:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 xiaofan1228 于 2020-3-8 14:58 编辑

题目
  1. >>> class Test:
  2.         x = MyDes(10, 'x')

  3. >>> test = Test()
  4. >>> y = test.x
  5. 正在获取变量: x
  6. >>> y
  7. 10
  8. >>> test.x = 8
  9. 正在修改变量: x
  10. >>> del test.x
  11. 正在删除变量: x
  12. 噢~这个变量没法删除~
  13. >>> test.x
  14. 正在获取变量: x
  15. 8
复制代码



答案
  1. class MyDes:
  2.     def __init__(self, initval=None, name=None):
  3.         self.val = initval
  4.         self.name = name

  5.     def __get__(self, instance, owner):
  6.         print("正在获取变量:", self.name)
  7.         return self.val

  8.     def __set__(self, instance, value):
  9.         print("正在修改变量:", self.name)
  10.         self.val = value

  11.     def __delete__(self, instance):
  12.         print("正在删除变量:", self.name)
  13.         print("噢~这个变量没法删除~")
复制代码


[b]


我的问题是,
为什么在上述题目中,delete方法没有办法删除变量x??

另:
“必须把描述符定义成另外一个类触发的类属性,不能定义到构造函数。”
这句话该怎么理解呢?
[/b]
最佳答案
2020-3-8 15:20:01
1. 因为在 MyDes 中的 __delete__ 方法没有写删除 x 的语句,所以 x 一直存在。

2. 就是必须在类中,但不能在函数中定义。

比如,这是正确的:

  1. class Test:
  2.     x = MyDes(10, 'x')
复制代码


这是错误的:

  1. class Test:
  2.     def __init__(self):
  3.         self.x = MyDes(10, 'x')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-8 15:20:01 | 显示全部楼层    本楼为最佳答案   
1. 因为在 MyDes 中的 __delete__ 方法没有写删除 x 的语句,所以 x 一直存在。

2. 就是必须在类中,但不能在函数中定义。

比如,这是正确的:

  1. class Test:
  2.     x = MyDes(10, 'x')
复制代码


这是错误的:

  1. class Test:
  2.     def __init__(self):
  3.         self.x = MyDes(10, 'x')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-8 15:34:48 | 显示全部楼层
zltzlt 发表于 2020-3-8 15:20
1. 因为在 MyDes 中的 __delete__ 方法没有写删除 x 的语句,所以 x 一直存在。

2. 就是必须在类中,但 ...
  1. class MyDes:
  2.     def __init__(self, value, name):
  3.         self.value = value
  4.         self.name  = name

  5.     def __get__(self, instance, owner):
  6.         print("Getting variable:%s" %self.name)
  7.         return self.value
  8.     def __set__(self, instance, value):
  9.         print("Setting variable:%s" %self.name)
  10.         self.value = value   
  11.     def __delete__(self, instance):
  12.         print("Deleting variable: %s" %self.name)
  13.         if self.value:
  14.             del self.value
  15.         else:
  16.             print("Delete no more!")

  17. class Test:
  18.     x = MyDes(10,"x")
复制代码


这个是我写的,本意是想说 del t.x 之后 再调用del  t.x会显示不能删除。
提示AttributeError: 'MyDes' object has no attribute 'value'
那其实__delete__是附带着值和实例x一起删除了?所以在第二次调用的时候 self 没有指向了 ?可以这么理解吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-8 15:39:39 | 显示全部楼层
xiaofan1228 发表于 2020-3-8 15:34
这个是我写的,本意是想说 del t.x 之后 再调用del  t.x会显示不能删除。
提示AttributeError: 'MyD ...

你这段代码只是删除了自身的属性 value,并没有删除自身
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-8 15:43:58 | 显示全部楼层
zltzlt 发表于 2020-3-8 15:39
你这段代码只是删除了自身的属性 value,并没有删除自身

那为什么if self.value会报错?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-8 17:31:20 | 显示全部楼层
xiaofan1228 发表于 2020-3-8 15:43
那为什么if self.value会报错?

因为 self.value 已经不存在了,它被删除了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-8 18:02:58 | 显示全部楼层
zltzlt 发表于 2020-3-8 17:31
因为 self.value 已经不存在了,它被删除了

啊 明白了,删除了不等于None...
改成try语句了

  1. class MyDes:
  2.     def __init__(self, value, name):
  3.         self.value = value
  4.         self.name  = name

  5.     def __get__(self, instance, owner):
  6.         print("Getting variable:%s" %self.name)
  7.         return self.value
  8.     def __set__(self, instance, value):
  9.         print("Setting variable:%s" %self.name)
  10.         self.value = value   
  11.     def __delete__(self, instance):
  12.         try:
  13.             del self.value
  14.             print("Deleting variable: %s" %self.name)
  15.         except AttributeError:
  16.             print("Delete no more!")

  17. class Test:
  18.     x = MyDes(10,"x")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-1 05:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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