-- 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