首页 > 代码库 > python ------- @property

python ------- @property

学习python 首次打卡,@property

@property给一个Screen对象加上widthheight属性,以及一个只读属性resolution

 

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
        
# test:
s = Screen()
s.width = 1024
s.height = 768
print(s.resolution)
assert s.resolution == 786432, 1024 * 768 = %d ? % s.resolution

 

python ------- @property