Discuss / Python / 要注意self._width和self.width是不同的,这里如果使用self.width的话由于属性名字与函数名相同会使其陷入死循环,前面加上一个_只是为了区分函数名,也可以用self.x

要注意self._width和self.width是不同的,这里如果使用self.width的话由于属性名字与函数名相同会使其陷入死循环,前面加上一个_只是为了区分函数名,也可以用self.x

Topic source

Superb来了

#1 Created at ... [Delete] [Delete and Lock User]

class Screen(object): @property def width(self): return self._width @width.setter def width(self,width): self._width=width @property def height(self): return self._height @height.setter def height(self,height): self._height=height @property def resolution(self): return self._height*self._width


  • 1

Reply