笔记-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的更多相关文章

  1. python 笔记2:python语法基础

    python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...

  2. Python笔记_1语法总结

    前言导读 本章知识点是我在最初期听python视频教程的时候整理总结的笔记 对python语法的认识对以后代码的解读有着很大的帮助. 1 新建python命名规则 新建项目名 :数字编号 项目名称 新 ...

  3. python语法笔记(四)

    1.对象的属性     python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统     对象的属性可能来自于其类定义,叫做类属性:还可能 ...

  4. python语法笔记(一)

    1. python中多个函数或者类定义可以放在一个.py 文件中,视为一个模块.模块的.py文件中,一般要写 if __name__ == '__mian__' 用来单独执行该模块内的某些函数. 2. ...

  5. 网络编程-Python高级语法-property属性

    知识点: 一.什么是property属性? 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法,Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最 ...

  6. wxpython 支持python语法高亮的自定义文本框控件的代码

    在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...

  7. (数据分析)第02章 Python语法基础,IPython和Jupyter Notebooks.md

    第2章 Python语法基础,IPython和Jupyter Notebooks 当我在2011年和2012年写作本书的第一版时,可用的学习Python数据分析的资源很少.这部分上是一个鸡和蛋的问题: ...

  8. 笔记-jinja2语法

    笔记-jinja2语法 1.      基本语法 控制结构 {% %} 变量取值 {{ }} 注释 {# #} 2.      变量 最常用的是变量,由Flask渲染模板时传过来,比如上例中的”nam ...

  9. 笔记-python lib-pymongo

    笔记-python lib-pymongo 1.      开始 pymongo是python版的连接库,最新版为3.7.2. 文档地址:https://pypi.org/project/pymong ...

随机推荐

  1. (生产)animate.css 动画库

    官网:https://daneden.github.io/animate.css/ Animate.css是一个有趣的,跨浏览器的css3动画库 用法 首先引入animate css文件:    &l ...

  2. pc端的企业网站(IT修真院test8)详解1-4

    今天完成的事情:(1,伪元素:before,:after的使用.2.table的使用(collapse的使用)3rgba的高级运用) 今天我主要完成test8-3的页面. header和footer都 ...

  3. Struts2_Path

    路径问题说明: struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径.index.jsp虽然可以用rederect方式解决,但redirect方式并非必要 ...

  4. kubernetes组件helm

    1.安装helm Helm由客户端helm命令行工具和服务端tiller组成,Helm的安装十分简单. 下载helm命令行工具到master节点node1的/usr/local/bin下(只需要在其中 ...

  5. GitLab-Runner 安装配置

    https://docs.gitlab.com/runner/install/linux-repository.html 直接看官方教程 systemctl status gitlab-runner. ...

  6. NYOJ(680),摘枇杷,(暴力,或者二分搜索)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=680 很巧妙的一个题目,就是看你的逆向思维,result 一定是max(a[i])~sum ...

  7. 定位webpack文件大小

    之前发现一个神器,记录一下,可以可视化webpack打包的每个js文件大小,这样对我们优化代码是有帮助的,有目标的 https://www.npmjs.com/package/webpack-bund ...

  8. jenkin+centos 7 环境搭建

    1.安装jenkins 首先安装好虚拟机和centos7操作系统  2.安装jdk 利用yum源来安装jdk(此方法不需要配置环境变量) 查看yum库中的java安装包 :yum -y list ja ...

  9. 2017.11.15 JavaWeb的学生体质管理系统

    (11)案例-----学生身体体质信息管理系统的开发 11.1 功能划分: 1.添加记录模块:完成向数据库添加新纪录 2.查询记录模块:完成将数据库的记录以网页的方式显示出来,一般采用有条件的查询 3 ...

  10. P2082 区间覆盖(加强版)

    题目 #include<iostream> #include<algorithm> #include<cstring> using namespace std; s ...