在此插入代码
class Screen(object):
@property def width(self): return(self._width)#这里 @width.setter def width(self, value): self._width = value#这里 @property def height(self): return(self._height)#这里 @height.setter def height(self, value): self._height = value#这里 @property def resolution(self): return(self._width * self._height)
请教老师,上面几个这里如果把_width之类的下划线去掉为什么就不能用了?出现这个错误RuntimeError: maximum recursion depth exceeded**
如果把 _width 的下划线去掉,那么在 getter/setter 方法里面操作 width 属性,实际上又是去调用同名的 getter/setter,形成无限递归。
_width
width
还是不明白,求详解
也不懂,求详解
解释就是: 这个GETTER方法是当你调用SELF.WIDTH的时候,就返回SELF._WIDTH 如果你在调用SELF.WIDTH的时候返回SELF.WIDTH,那不就循环了么
5L解释的好,我先还以为是双下划线,为了访问限制什么的。 加个下划线算是个小技巧?
Sign in to make a reply
展大大大侠
class Screen(object):
请教老师,上面几个这里如果把_width之类的下划线去掉为什么就不能用了?出现这个错误RuntimeError: maximum recursion depth exceeded**