tiger吴 发表于 2020-6-22 17:53:37

关于__setattr__


class Rectangle:
    def __init__(self,width=0,height=0):
      self.width=width
      self.height=height
    def __setattr__(self,name,value):
      if name=='square':
            self.width=value
            self.height=value
      else:
            super().__setattr__(name,value)
    def getAre(self):
      return self.width*self.height

r1=Rectangle('square',3)
print(r1.getAre())
为什么代码跑起来结果是打印了三次square

tiger吴 发表于 2020-6-22 17:56:16

还有为什么else上面的赋值操作没有改,程序却没有无限递归,

qiuyouzhi 发表于 2020-6-22 17:59:07

__init__是实例化时触发的,__setattr__是给实例属性赋值时触发的...
所以,self.width是"square",self.height是3
也就是把square重复三遍。

永恒的蓝色梦想 发表于 2020-6-22 18:09:41

楼上正解

tiger吴 发表于 2020-6-22 18:24:38

qiuyouzhi 发表于 2020-6-22 17:59
__init__是实例化时触发的,__setattr__是给实例属性赋值时触发的...
所以,self.width是"square",self.h ...

意思是实例赋值时,__setattr__的参数会自动传入并覆盖__init__的参数?

tiger吴 发表于 2020-6-22 18:27:24

qiuyouzhi 发表于 2020-6-22 17:59
__init__是实例化时触发的,__setattr__是给实例属性赋值时触发的...
所以,self.width是"square",self.h ...

__setattr__下面有赋值啊,又为什么没用

qiuyouzhi 发表于 2020-6-22 18:49:00

tiger吴 发表于 2020-6-22 18:27
__setattr__下面有赋值啊,又为什么没用

__setattr__根本就没有被触发...

Twilight6 发表于 2020-6-22 19:16:31




为什么打印三次呢,因为返回的是 self.width*self.height ,而你的传入参数是 'square'和 3 ,也就是:
self.width = 'square'
self.height = 3
所以字符串 * 3 就是将三个相同的字符串拼接一起,所以返回了3次的 square


第二个问题:

你可能没理解 __setattr__(self,name,value) 里面的name 参数 和 value 参数代表的是什么

name 代表的就是赋值的变量名, value 代表的是 赋值的值,简单点说就是 等号左边的是name 右边的是 value

然后当你 r1=Rectangle('square',3)将实例化对象 r1会自动调用 __init__ 方法

而 __init__ 方法里面有两个赋值操作:
self.width=width
self.height=height
这两个赋值操作会调用直接的 __setattr__ 先来看第一个 self.width=width这里调用了自己的 __setattr__ 方法

name 指的就是 等号左边的 width ,而 value 是等号右边的 就是你传入的 'square' 字符串,进入 __setattr__ 方法 进行判断 name 是否等于 'square'

因为这里的 name = 'width' 不等于 'square' 所以不满足 if 条件执行 else

else 用super 调用基类的 __setattr__方法 来给 width 赋值为 'square'

同理 self.height=height 也是一样的

所以不会导致 无限递归调用 __setattr__方法




页: [1]
查看完整版本: 关于__setattr__