我这是刚入门的python 写了如下代码 出现如下问题 请问大佬们为什么
-- coding: utf-8 --
class Screen(object):
@property
def width(self):
return self.width
@width.setter
def width(self,value):
return self._width is value
@property
def hegiht(self):
return self._hegiht
@hegiht.setter
def hegiht(self,value):
return self._hegiht is value
@property
def resolution(self):
return self._width*self._hegiht
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
print('测试通过!')
else:
print('测试失败!')
错误: s.width = 1024
File "3.py", line 8, in width
return self._width is value
AttributeError: 'Screen' object has no attribute '_width'
去掉_也是报错: s.width = 1024
File "3.py", line 8, in width
return self.width is value
File "3.py", line 5, in width
return self.width
File "3.py", line 5, in width
return self.width
File "3.py", line 5, in width
return self.width
[Previous line repeated 994 more times]
RecursionError: maximum recursion depth exceeded
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/625/viewspace-3131831/ ,如需转载,请注明出处,否则将追究法律责任。