property函数

property() 内建函数有四个参数,它们是:
property(fget=None, fset=None, fdel=None, doc=None)

__metaclass__ = type
class Rectangle:
    def __init__(self):
        self.width = 0
        self.height = 0
    def setSize(self, size):
        self.width, self.height = size
    def getSize(self):
        return self.width, self.height
    size = property(getSize, setSize)   # 这样一来就不再需要担心是怎么实现的
                                        # 可以用同样的方式处理width,height
                                        # 和size。
>>> r = Rectangle()
>>> r.width = 10
>>> r.height = 5
>>> r.size
(10, 5)
>>> r.size = 150, 100
>>> r.width
150

property函数可以用0,1,2,3或者4个参数来调用。
如果没有参数,产生的属性既不可读,也不可写。
如果只使用一个参数调用(一个取值方法),产生的属性是只读的第3个参数(可选)是
   一个用于删除特性的方法(它不要参数)
第4个参数(可选)是一个文档字符串。
property的4个参数分别被叫做fget, fset, fdel和doc

你可能感兴趣的:(python函数)