您的位置:首页 > 编程语言 > Python开发

python中property干什么用的?

2015-06-30 17:03 691 查看
先来段官方文档压压惊。。
property(fget=None, fset=None, fdel=None, doc=None)
Return a property attribute.

fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del’ing, an attribute. Typical use is to define a managed attribute x:

class C:
def __init__(self):
self._x = None

def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")


If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.



property的用法是为你的类设置一个属性值。第一个参数设定获取属性值的方法,第二个参数设定设置这个属性值的方法,第三个参数设定删除这个属性值的方法,第四个参数是文档。c.x会调用第一个参数的方法,c.x = value 调用第二个方法,del c.x调用第三个方法。这样
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: