Property - 特性(Python)
Property - Python 特性
不同的书籍对 property 一词的翻译有所不同, 我们将 property 翻译成 '特性' 以区别于 attribute 一词.
先看看 property 类在 Python 中的定义,
结构,
class property(object):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
...
...
...
简略表示为,
class property(fget=None, fset=None, fdel=None, doc=None) 其中参数,
fget 是一个'取值'(get attribute value)函数
fset 是一个'设值'(set attribute value)函数
fdel 是一个'删除值'(delete attribute value)函数
doc 对所返回的 property attribute 的描述(docstring) property 的典型用法(以通过 property 管理属性 A.a 为例),
class A(object):
def __init__(self):
self._a = None def geta(self):
""" This docstring in fget method """
print('geta is called')
return self._a def seta(self, value):
print('seta is called')
self._a = value def dela(self):
print('dela is called')
del self._a a = property(geta, seta, dela, "Typical use of \'property\'")
#a = property(geta, seta, dela) if __name__ == '__main__':
tester1 = A()
print(vars(A)) #
print(vars(tester1)) #
tester1.a = 'hello world' #
print(tester1.a) #
del tester1.a #
print(vars(A)) #
print(vars(tester1)) #
print(A.a.__doc_) #
tester1.a = 'hello world2' #
print(vars(tester1)) #
print(tester1.a) Output,
{'__module__': '__main__', '__init__': <function A.__init__ at 0x02DB62B8>, 'geta': <function A.geta at 0x02DB6270>,
'seta': <function A.seta at 0x02DB6228>, 'dela': <function A.dela at 0x02DB61E0>, 'a': <property object at 0x02DB92A0>,
'__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
#1 类对象中存在被命名为 'a' property 对象 {'_a': None} #2 实例实际所拥有时是 _a,
seta is called #3 会调用 'seta'
geta is called
hello world #4 将调用 'geta'
dela is called #5 调用 'dela'
{'__module__': '__main__', '__init__': <function A.__init__ at 0x02DB62B8>, 'geta': <function A.geta at 0x02DB6270>,
'seta': <function A.seta at 0x02DB6228>, 'dela': <function A.dela at 0x02DB61E0>, 'a': <property object at 0x02DB92A0>,
'__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None} {} #6 实例的 '_a' 属性被删除
return self._a, AttributeError: 'A' object has no attribute '_a' #7 虽然调用了 del 类对象中仍然存在被命名为 'a' property 对象 Typical use of 'property' #8 第四个参数 docstring 存在, property a 的 __doc__ 属性为所提供 docstring 参数
This docstring in fget method #8 没有设置第四个该参数, 则 property a 的 __doc__ 会引用 geta 的 docstring seta is called #9 在次设置 property a
{'_a': 'hello world2'} #9 '_a' 回到 '实例' 属性中
geta is called #
hello world2 # 注, abc = A(), abc.a 将调用 'geta' ; abc.a = 3 会调用 'seta'; del abc.a 调用 'dela'
若第四个参数 docstring 存在, property a 的 __doc__ 属性为所提供 docstring 参数(A.a.__doc__);
若没有设置该参数, 则 property a 的 __doc__ 会引用 geta 的 docstring.
If abc is an instance of A, abc.a will invoke the getter, abc.a = value will invoke the setter and del abc.a the deleter.
If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget’s docstring (if it exists).
property 类, 更实用的是 以装饰器 @property 来实现 '只读' readonly 属性(若实例中没有实现 a.setter, a 便成为'只读' readonly 属性),
例子(与上例等价形式的 @property 的实现), class B(object):
def __init__(self):
self._a = None @property # 将 'a' 装饰成 property
def a(self):
""" This docstring in fget method"""
print('fget is called')
return self._a # The @property decorator turns the a() method into a “getter”
# for a read-only attribute with the same name, and it sets the
# docstring for a to “This docstring in fget method”
# 如类中没有实现 a.setter, a 便成为'只读' readonly 属性. @a.setter # 装饰 property a 的 setter
def a(self, value):
print('fset is called')
self._a = value @a.deleter # 装饰 property a 的 deleter
def a(self):
print('fdel is called')
del self._a *** 注, 务必保证 setter 和 deleter 所装饰的 '函数' 与 被装饰的 '特性' 函数 名字一致
Be sure to give the additional functions the same name as the original property (a in this case.)
Property - 特性(Python)的更多相关文章
- Python深入浅出property特性属性
导语 在Java中,通常在类中定义的成员变量为私有变量,在类的实例中不能直接通过对象.属性直接操作,而是要通过getter和setter来操作私有变量. 而在Python中,因为有property这个 ...
- 类的property特性
目录 什么是 property特性 简单示例 property属性的两种方式 装饰器 类属性方式 property+类的封装 应用 私有属性添加getter和setter方法 使用property升级 ...
- 类的封装,property特性,类与对象的绑定方法和非绑定方法,
类的封装 就是把数据或者方法封装起来 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度(快门就是傻瓜相机为傻瓜们提供的方法,该方法将内部复杂的照相功能都隐藏起来了,比如你 ...
- day36 类的三大特性---封装以及Property特性
目录 类的封装 如果真的要拿 类的property特性 setter & deleter 类属性用法 类与对象的绑定方法和非绑定方法 对象方法&类方法&静态方法 隐藏模块内的函 ...
- python 旧类中使用property特性的方法
在python中,我们可以拦截对象的所有特性访问.通过这种拦截的思路,我们可以在旧式类中实现property方法. __getattribute__(self, name) #当特性name被访问时自 ...
- Python面向对象之封装、property特性、绑定方法与非绑定方法
一.封装 ''' 1.什么封装 封:属性对外是隐藏的,但对内是开放的(对内是开放的是因为在类定义阶段这种隐藏已经发生改变) 装:申请一个名称空间,往里装入一系列名字/属性 2.为什么要封装 封装数据属 ...
- day22-类的封装、property特性以及绑定方法与非绑定方法
目录 类的封装 两个层面的封装 第一个层面 第二个层面 封装的好处 私有模块 类的propertry特性 setter 和 deleter 类与对象的绑定方法与非绑定方法 类的封装 将类的属性或方法隐 ...
- 封装、property特性及绑定与非绑定方法
1.封装 (1)什么是封装? 封:属性对外是隐藏的,但对内是开放的: 装:申请一个名称空间,往里面装入一系列名字/属性 (2)为什么要封装? 封装数据属性的目的 首先定义属性的目的就是为了给类外部的使 ...
- property特性
什么是property property是一种特殊属性,访问他时会执行一段功能然后返回值 class People: def __init__(self,name,weight,height): se ...
随机推荐
- FullPage.js-基于 jQuery 的插件全屏滚动插件
fullPage.js 是一个基于 jQuery 的插件,它能够很方便.很轻松的制作出全屏网站.如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容, ...
- rest实践3
1.从mongodb的数据实体Document中获取其中一个字段的值,即例如:doc.getString("pid"),直接显示value. 2.当从网络上的网址url的图片直接弄 ...
- 【转】程序员"青春饭"问题之我见
1. 问题描述问题1: 什么是程序员?在本文中程序员的定义为: 拥有编程技能,在IT.互联网公司打工的IT从业人员.程序员与很多行业最大的不同是该行业的形成时间短:1954年第一台计算机才诞生,而中医 ...
- Window同一电脑配置多个git公钥
前言 配置多个本地ssh-key之前,先初始化下GIt环境哦! 可以参照:https://www.cnblogs.com/poloyy/p/12185132.html 执行前两步就好啦 本地生成两个s ...
- 高通量计算框架HTCondor(五)——分布计算
目录 1. 正文 1.1. 任务描述文件 1.2. 提交任务 1.3. 返回结果 2. 相关 1. 正文 1.1. 任务描述文件 前文提到过,HTCondor是通过condor_submit命令将提交 ...
- Java语法进阶16-Lambda-Stream-Optional
Lambda 大年初二,大门不出二门不迈.继续学习! 函数式接口 Lambda表达式其实就是实现SAM接口的语法糖,所谓SAM接口就是Single Abstract Method,即该接口中只有一个抽 ...
- [bzoj3143] [洛谷P3232] [HNOI2013] 游走
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- Cobalt_Strike扩展插件
Cobalt_Strike3.14下载: https://download.csdn.net/download/weixin_41082546/11604021 https://github.com/ ...
- 小白学Java:内部类
目录 小白学Java:内部类 内部类的分类 成员内部类 局部内部类 静态内部类 匿名内部类 内部类的继承 内部类有啥用 小白学Java:内部类 内部类是封装的一种形式,是定义在类或接口中的类. 内部类 ...
- STM32学习笔记:基础例子
本例子代码参考了STM32库开发实战指南中的代码,由于使用的板子是尚学STM32F103ZET6,为了配合板上已有资源,也参考了其配套代码.为了便于书写文本,我尽量将代码都写到了一个文件中,这种方式是 ...