鱼C论坛

 找回密码
 立即注册
查看: 1174|回复: 1

小蠢鱼

[复制链接]
发表于 2018-2-8 13:50:59 | 显示全部楼层 |阅读模式

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

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

x
问题一.对于第四行的 return super(). 不太明白,这个时候调用object.__getattribute__ 有什么用??

  1. class C:
  2.         def __getattribute__(self,name):
  3.                 print('getattribute')
  4.                 return super().__getattribute__(name)
  5.         def __getattr__(self):
  6.                 print('__getattr__')
  7.         def __setattr__(self,name,value):
  8.                 print('setattr')
  9.                 super().__setattr__(name,value)
  10.         def __delattr__(self,name):
  11.                 print('delattr')
  12.                 super().__delattr__(name)
复制代码

问题二,为什么又是调用了object.__setattr__ 就可以避免死循环
  1. class Rectangle:
  2.         def __init__(self,width = 0,length = 0):
  3.                 self.width = width
  4.                 self.length = length
  5.         def __setattr__(self,name,value):
  6.                 if name == 'square':
  7.                         self.width = value
  8.                         self.length = value
  9.                 #这是一种会导致死循环的写法
  10.                 else:
  11.                         self.name = value
  12.                 #下面是另外一种避免死循环的写法
  13.                 else:
  14.                     super().__setattr__(name,value)
  15.         def getare(self):
  16.             return self.width*self.length
复制代码


感觉问题一和问题二 都差不多 实在搞不懂 调用object的方法就这么厉害?
请大神解答一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-8 16:18:30 | 显示全部楼层
问题二已解决,谁知道问题一吗....
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-27 21:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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