×
Python 基础教程Python 简介Python 环境搭建Python 中文编码Python 基础语法Python 变量类型Python 运算符Python 条件语句Python 循环语句Python While循环语句Python for 循环语句Python 循环嵌套Python break 语句Python continue 语句Python pass 语句Python 数字Python 字符串Python 列表(Lists)Python 元组Python 字典(Dictionary)Python 日期和时间Python 函数Python 模块Python 文件I/OPython File(文件) 方法Python 异常处理Python OS 文件/目录方法Python 内置函数

Python 高级教程

Python 面向对象Python 正则表达式Python CGI编程python MysqlPython SMTPPython 多线程Python XML解析python GUI编程(Tkinter)Python 2.x与3​​.x版本区别Python IDEPython JSONPython 100例

Python property() 函数


Python 内置函数Python 内置函数


描述

property() 函数的作用是在新式类中返回属性值。

语法

以下是 property() 方法的语法:

class property([fget[, fset[, fdel[, doc]]]])

参数

  • fget -- 获取属性值的函数
  • fset -- 设置属性值的函数
  • fdel -- 删除属性值函数
  • doc -- 属性描述信息

返回值

返回新式类属性。

实例

定义一个可控属性值 x

class C(object): 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.")

如果 cC 的实例化, c.x 将触发 getter,c.x = value 将触发 setter , del c.x 触发 deleter。

如果给定 doc 参数,其将成为这个属性值的 docstring,否则 property 函数就会复制 fget 函数的 docstring(如果有的话)。

将 property 函数用作装饰器可以很方便的创建只读属性:

class Parrot(object): def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage

上面的代码将 voltage() 方法转化成同名只读属性的 getter 方法。

property 的 getter,setter 和 deleter 方法同样可以用作装饰器:

class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x

这个代码和第一个例子完全相同,但要注意这些额外函数的名字和 property 下的一样,例如这里的 x。


Python 内置函数Python 内置函数


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)