鱼C论坛

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

[知识点备忘] 第072讲:类和对象(XV)

[复制链接]
发表于 2022-8-25 04:27:02 | 显示全部楼层 |阅读模式
购买主题 已有 20 人购买  本主题需向作者支付 5 鱼币 才能浏览
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-9 09:39:49 | 显示全部楼层
过了个愉快的国庆节,继续来学习Python!这节课讲了一个较难理解的函数——property()函数,该函数用于返回一个property属性对象,使之成为“托管属性”,全权代理该类中的私有属性,从而对其进行访问或修改,实现起来比用“魔法方法三件套”(__getattr__()、__setattr__()、__delattr__())更简单。此外,作为装饰器也是property()函数的经典应用,可以轻松创建只读属性,因为property()的第一个参数正是获取私有属性的函数。property属性对象还提供了getter()、setter()、deleter()三个方法,对应该函数的三个参数接口,使用相应方法作为装饰器殊途同归。看来,property()函数果然妙用非凡,真是“令菜鸟落泪,大神陶醉”啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-7 21:41:14 | 显示全部楼层
本帖最后由 Ensoleile 于 2023-1-10 00:29 编辑

property()
  1. #class property(fget=None,fset=None,fdel=None,doc=None)   P159   Build-in Class
  2. #property()函数用于返回一个property属性对象
  3. class C:
  4.     def __init__(self):
  5.         self._x = 250
  6.     def getx(self):
  7.         return self._x
  8.     def setx(self, value):
  9.         self._x = value
  10.     def delx(self):
  11.         del self._x
  12.     x = property(getx, setx, delx)

  13. c = C()
  14. print(c.x)#250
  15. c.x = 520
  16. print(c.__dict__)#{'_x': 520}
  17. del c.x
  18. print(c.__dict__)#{}

  19. #利用__getattr__()、__setattr__()、__delattr__()实现相同目的
  20. class D:
  21.     def __init__(self):
  22.         self._x = 250
  23.     def __getattr__(self, item):
  24.         if item == 'x':
  25.             return self._x
  26.         else:
  27.             super().__getattr__(item)
  28.     def __setattr__(self, key, value):
  29.         if key == 'x':
  30.             super().__setattr__('_x', value)
  31.         else:
  32.             super().__setattr__(key, value)
  33.     def __delattr__(self, item):
  34.         if item == 'x':
  35.             super().__delattr__('_x')
  36.         else:
  37.             super().__delattr__(item)

  38. d = D()
  39. print(getattr(d, 'x'))#250
  40. d.x = 520
  41. print(d.__dict__)#{'_x': 520}
  42. del d.x
  43. print(d.__dict__)#{}
  44. #propert()函数第一个优点是简化类似遮遮掩掩的操作

  45. #讲property()函数做装饰器使用,会让创建只读属性工作变得极为简单
  46. class E:
  47.     def __init__(self):
  48.         self._x = 250
  49.     @property
  50.     def x(self):
  51.         return self._x

  52. e = E()
  53. print(e.x)#250
  54. try:
  55.     e.x = 520
  56. except AttributeError as e:
  57.     print(e)#can't set attribute
  58. #只能读取属性,不能修改,原因:吧装饰器改写成正常情况应该是:
  59. class E:
  60.     def __init__(self):
  61.         self._x = 250
  62.     def x(self):
  63.         return self._x
  64.     x = property(x)#位置参数传x对应第一个参数fget
  65. # 所以只读。
  66. # 想要继续传入其他两个参数,property属性对象(由property函数返回,根据语法糖解析是传给了x)提供了getter、setter和deleter三个方法,这些方法对应property()函数的三个参数接口
  67. class E:
  68.     def __init__(self):
  69.         self._x = 250
  70.     @property
  71.     def x(self):
  72.         return self._x
  73.     @x.setter
  74.     def x(self, value):
  75.         self._x = value
  76.     @x.deleter
  77.     def x(self):
  78.         del self._x

  79. e = E()
  80. print(e.x)#250
  81. e.x = 520
  82. print(e.__dict__)#{'_x': 520}
  83. del e.x
  84. print(e.__dict__)#{}
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-4-6 14:51:04 | 显示全部楼层

您好,请问第32行语句是什么意思?它只是说了寻找父类的__setattr__()方法,后面的'_x'和value实现了什么操作呢?实在是想不明白
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-12 21:15:58 | 显示全部楼层
就是传给父类Object类,让他报错用的。这里咱们自己raise一个错误也可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-4 16:36:31 | 显示全部楼层
思维导图啥时候上
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-7 17:20:45 | 显示全部楼层
看视频自己总结的时间到了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-5 16:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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