property函数

property 4个参数, fget、 fset、 fdel、 doc
实际上讲 get  set del 方法绑定在一个成员变量上
 1 class Rectangle(object):
 2     def __init__(self):
 3         self.width = 0
 4         self.height = 0
 5         
 6     def setSize(self, size):
 7         self.width, self.height = size
 8     
 9     def getSize(self):
10         return self.width, self.height
11     
12     def delSize(self):
13         print('delete')
14         del self.width, self.height
15     
16     size = property(getSize, setSize, delSize, 'Tester')
17 
18 if __name__ == '__main__':
19     rt = Rectangle()
20     print(rt.size)
21     rt.width = 10
22     rt.height = 20
23     print(rt.size)
24     rt.size = 100, 50
25     print(rt.size)
26     del rt.size

 

posted @ 2013-06-10 01:01  Roger|  阅读(174)  评论(0编辑  收藏  举报