笔记-python-语法-property
笔记-python-语法-property
1. property
看到@property,不明白什么意思,查找文档了解一下。
1.1. property类
proerty是python自带的类,在
>>> property
<class 'property'>
>>> dir(property)
['__class__', '__delattr__', '__delete__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__isabstractmethod__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']
>>> property.__class__
<class 'type'>
官方文档:
class property(fget=None, fset=None, fdel=None, doc=None)
Return a property attribute.
fget是一个获取属性值的函数,fset是设定值的函数,fdel是删除值的函数。doc创建属性的说明文档。
案例:
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.")
运行结果如下:
>>> C.x
<property object at 0x000000BC05B078B8>
>>> dir(C)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'delx', 'getx', 'setx', 'x']
解释:上面的代码给类C添加了一个属性x,而x是一个property对象,这样做的结果是可以像下面这样操作self._x
>>> a = C()
赋值
>>> a.x = 6
>>> a._x #6
删除
>>> del(a.x)
>>> a.x
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
a.x
File "E:\python\person_code\interview questions\built-in.py", line 47, in getx
return self._x
AttributeError: 'C' object has no attribute '_x'
>>> a._x
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
a._x
AttributeError: 'C' object has no attribute '_x'
最终的结果就是简化了实例对象属性的操作代码。
再看下面的代码:
A property object has getter, setter, and deleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function.
This is best explained with an example:
class C:
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
这里使用的装饰符实现,功能与上例没什么区别,但代码更简洁。
注意seter和deleter并不是必需的。
怎么实现的就不讨论了,总而言之它可以简化实例属性操作,简化代码。
笔记-python-语法-property的更多相关文章
- python 笔记2:python语法基础
python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...
- Python笔记_1语法总结
前言导读 本章知识点是我在最初期听python视频教程的时候整理总结的笔记 对python语法的认识对以后代码的解读有着很大的帮助. 1 新建python命名规则 新建项目名 :数字编号 项目名称 新 ...
- python语法笔记(四)
1.对象的属性 python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统 对象的属性可能来自于其类定义,叫做类属性:还可能 ...
- python语法笔记(一)
1. python中多个函数或者类定义可以放在一个.py 文件中,视为一个模块.模块的.py文件中,一般要写 if __name__ == '__mian__' 用来单独执行该模块内的某些函数. 2. ...
- 网络编程-Python高级语法-property属性
知识点: 一.什么是property属性? 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法,Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最 ...
- wxpython 支持python语法高亮的自定义文本框控件的代码
在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...
- (数据分析)第02章 Python语法基础,IPython和Jupyter Notebooks.md
第2章 Python语法基础,IPython和Jupyter Notebooks 当我在2011年和2012年写作本书的第一版时,可用的学习Python数据分析的资源很少.这部分上是一个鸡和蛋的问题: ...
- 笔记-jinja2语法
笔记-jinja2语法 1. 基本语法 控制结构 {% %} 变量取值 {{ }} 注释 {# #} 2. 变量 最常用的是变量,由Flask渲染模板时传过来,比如上例中的”nam ...
- 笔记-python lib-pymongo
笔记-python lib-pymongo 1. 开始 pymongo是python版的连接库,最新版为3.7.2. 文档地址:https://pypi.org/project/pymong ...
随机推荐
- C++ Knowledge series Template & Class
Function Function is composed of name, parameter (operand, type of operand), return value, body with ...
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- Office加载项对Excel进行读写操作
转载自我的个人主页 前言 在开发ExcelWeb插件的时候,一大亮点就是可以在web项目中操作Excel,读取Excel的内容,也可以将服务端的数据写入的 Excel中,大大方便的用户使用Excel, ...
- 力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...
- 45. 腾讯面试题: 使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据
题目:使用hashmap 插入数据,怎么样依照插入数据的顺序输出数据 分析: 使用hashmap插入数据,数据的顺序会改变.能够写个小程序试试. 那怎么样依照插入的顺序输出呢? 方法一: 这是我第一时 ...
- Uva 10791 最小公倍数的最小和 唯一分解定理
题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...
- python 下实现window 截图
首先安装PIL库,因为PIL官网没有支持python3.6的PIL库我想在3.X中实现,因此使用pip安装pillow pip install pillow 安装 安装完成后,from PIL imp ...
- frcnn_train_data_param的distort_param实现
frcnn_train_data_param frcnn_train_data_param { source: "./data/train_list.txt" root_folde ...
- Linux中的/etc/nologin问题
/etc/nologin 文件给系统管理员提供了在 Linux 系统维护期间禁止用户登陆的方式. 如果系统中存在 /etc/nologin 文件那么普通用户登陆就会失败. 这是一种提高安全性和防止数据 ...
- 第51章 设置FLASH的读写保护及解除—零死角玩转STM32-F429系列
第51章 设置FLASH的读写保护及解除 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...