|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- class R:
- def __init__(self):
- self.counter = 0
- def __setattr__(self, name, value):
- self.counter = value
复制代码
>>> r = R()
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
r = R()
File "<pyshell#37>", line 3, in __init__
self.counter = 0
File "<pyshell#37>", line 5, in __setattr__
self.counter = value
File "<pyshell#37>", line 5, in __setattr__
self.counter = value
File "<pyshell#37>", line 5, in __setattr__
self.counter = value
[Previous line repeated 326 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
因self.counter = 0赋值操作触发__setattr__, 而其中self.counter = value又是赋值操作,所以造成死循环。
- class R:
- def __init__(self):
- self.counter = 0
- def __setattr__(self, name, value):
- self.counter = self.counter + value
复制代码
>>> r = R()
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
r = R()
File "<pyshell#34>", line 3, in __init__
self.counter = 0
File "<pyshell#34>", line 5, in __setattr__
self.counter = self.counter + value
AttributeError: 'R' object has no attribute 'counter'
请问为什么程式码只有些改变,为什么却变成没有属性counter呢?真得想不懂? ? ?
counter属性还没定义出去,先进行了赋值,赋值里对counter属性操作,当然没定义
|
|