# coding=utf-8 ''' 装饰器(decorator)可以给函数动态加上功能,对于类的方法,装饰器一样起作用。Python内置的@property装饰器就是负责把一个方法变成属性调用的: @property:把一个getter方法变成属性 @score.setter:负责把一个setter方法变成属性赋值 ''' class Screen(object): #读属性 @property def width(self): return self.value_of_width #写属性 @width.setter def width(self, value): self.value_of_width = value @property def height(self): return self.value_of_height @height.setter def height(self, value): self.value_of_height = value @property def resolution(self): return self.value_of_height*self.value_of_width if __name__ == '__main__': s = Screen() s.width = 1024 s.height = 768 print(s.resolution) assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution