码迷,mamicode.com
首页 > 编程语言 > 详细

python ------- @property

时间:2017-05-12 13:23:58      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:pre   属性   width   self   int   elf   对象   ret   assert   

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

标签:pre   属性   width   self   int   elf   对象   ret   assert   

原文地址:http://www.cnblogs.com/jantzy/p/6844892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!