内置装饰器二:@property
property 装饰器的作用
property 装饰器将方法包装成属性,将私有属性公有化,此属性只能被读取。相当于实现get方法的对象
class People:
def __init__(self, identity_number):
self._identity_number = identity_number
@property # 只读
def age(self):
return self._age
@age.setter # 写
def age(self, value):
if not isinstance(value, int):
raise ValueError("age must be an integer!")
if value < 0:
raise ValueError("age must more than 0")
self._age = value
@age.deleter # 删除
def age(self):
del self._age
@property
def get_identity_number(self):
return self._identity_number
#In [22]: p = People(123456)
#In [23]: p.age = 18
# In [24]: p.age
# Out[24]: 18
# In [25]: del p.age
# In [26]: p.age
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-3523b116dc0e> in <module>()
----> 1 p.age
<ipython-input-21-de21f31a52ce> in age(self)
5 @property # 只读
6 def age(self):
----> 7 return self._age
8
9 @age.setter # 写
AttributeError: 'People' object has no attribute '_age'
# In [27]: p.age = 18
# In [28]: p.age = 18.6
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-8a03211dcd49> in <module>()
----> 1 p.age = 18.6
<ipython-input-21-de21f31a52ce> in age(self, value)
10 def age(self, value):
11 if not isinstance(value, int):
---> 12 raise ValueError("age must be an integer!")
13 if value < 0:
14 raise ValueError("age must more than 0")
ValueError: age must be an integer!
# In [29]: p.age = '11'
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-b6d20eff2848> in <module>()
----> 1 p.age = '11'
<ipython-input-21-de21f31a52ce> in age(self, value)
10 def age(self, value):
11 if not isinstance(value, int):
---> 12 raise ValueError("age must be an integer!")
13 if value < 0:
14 raise ValueError("age must more than 0")
ValueError: age must be an integer!
# In [30]: p.age = -1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-30-47db6d5817ed> in <module>()
----> 1 p.age = -1
<ipython-input-21-de21f31a52ce> in age(self, value)
12 raise ValueError("age must be an integer!")
13 if value < 0:
---> 14 raise ValueError("age must more than 0")
15 self._age = value
16
ValueError: age must more than 0
# In [31]:
会把成员函数x转换为getter,相当于做了x = property(); x = x.getter(x_get)
- @property表示只读。
- 同时有@property和@x.setter表示可读可写。
- 同时有@property和@x.setter和@x.deleter表示可读可写可删除。
参考资料:https://docs.python.org/3/library/functions.html?highlight=property#property
http://blog.willdx.me/web/面向对象进阶.html
内置装饰器二:@property的更多相关文章
- python基础语法16 面向对象3 组合,封装,访问限制机制,内置装饰器property
组合: 夺命三问: 1.什么是组合? 组合指的是一个对象中,包含另一个或多个对象. 2.为什么要用组合? 减少代码的冗余. 3.如何使用组合? 耦合度: 耦: 莲藕 ---> 藕断丝连 - 耦合 ...
- property内置装饰器函数和@name.setter、@name.deleter
# property # 内置装饰器函数 只在面向对象中使用 # 装饰后效果:将类的方法伪装成属性 # 被property装饰后的方法,不能带除了self外的任何参数 from math import ...
- python进阶04 装饰器、描述器、常用内置装饰器
python进阶04 装饰器.描述器.常用内置装饰器 一.装饰器 作用:能够给现有的函数增加功能 如何给一个现有的函数增加执行计数的功能 首先用类来添加新功能 def fun(): #首先我们定义一个 ...
- python内置装饰器
前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...
- classmethod、staticclassmethod内置装饰器函数
# method 英文是方法的意思 # classmethod 类方法 # 当一个类中的方法中只涉及操作类的静态属性时,此时在逻辑上,我们想要直接通过类名就可以调用这个方法去修改类的静态属性,此时可以 ...
- Python内置装饰器@property
在<Python装饰器(Decorators )>一文中介绍了python装饰器的概念,日常写代码时有一个装饰器很常见,他就是内置的@property. 我们一步步的来接近这个概念. 一个 ...
- 面向对象——组合、封装、访问限制机制、property内置装饰器
面向对象--组合.封装.访问限制机制.property 组合 什么是组合? 组合指的是一个对象中,包含另一个或多个对象 为什么要组合? 减少代码的冗余 怎么用组合? # 综合实现 # 父类 class ...
- python之内置装饰器(property/staticmethod/classmethod)
python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...
- Python 内置装饰器
内置的装饰器 内置的装饰器和普通的装饰器原理是一样的,只不过返回的不是函数,而是类对象,所以更难理解一些. @property 在了解这个装饰器前,你需要知道在不使用装饰器怎么写一个属性. d ...
随机推荐
- 相机拍摄时最重要的三个参数——光圈、快门、ISO
注:这篇文章我四年前发布在其他地方,现在移过来. 如果你对相机只有很少了解,那么看这篇文章再好不过啦,我结合很多资料,力图用最通俗易懂的方式进行讲解. 相机拍摄时最重要的3个参数就是——光圈.快门.I ...
- [SoapUI] 获取当前时间包括年月日时分秒来作为命名
import java.text.SimpleDateFormat GregorianCalendar calendar = new GregorianCalendar() def dateForma ...
- 将C语言宏定义数值转换成字符串!
将C语言宏定义转换成字符串! 摘自:https://blog.csdn.net/happen23/article/details/50602667 2016年01月28日 19:15:47 六个九十度 ...
- setTimeout setInterval 详解
http://www.jb51.net/article/74606.htm var tttt=setTimeout('northsnow()',1000);//执行一次 clearTimeou ...
- IE浏览器调用jquery需要注意的小问题
今天在进行前端重构的时候发现了一个非常奇怪的浏览器兼容性问题,我想在网页上放一个JS的特效,于是下载了jquery-easyui,经过修改完成所需要的效果后,准备放入项目中,发现在IE浏览器中无法运行 ...
- document.body和document.documentElement区别
1.document.documentElement表示文档节点树的根节点,即<html> document.body是body节点 2. 页面具有 DTD,或者说指定了 DOCTYPE ...
- android apk签名原理
//这个md5跟腾讯的对应 public Signature getPackageSignature( ){ Context context=getContext(); String packageN ...
- 状态机中的RAM注意的问题--减少扇出的办法
可能我不会抓紧时间,所以做事老是很慢.最近在整维特比译码过程深感自己有这样的毛病. 每天会有一点进展,但是却是一天的时间,感觉别人都做起事情来很快.可能这个东西有点难,做 不做得出来都不要紧,但我的想 ...
- 如何用命令行将我的Phonegap环境更新到最新版本?
从npm安装的Phonegap(version > 3.0),更新命令如下 npm update -g phonegap 检查当前本机环境的最新版本 phonegap -v 检查npm的最新ph ...
- day01(静态、代码块、类变量和实类变量辨析 )
静态: 关键字:static 概述: 使用static关键字修饰的成员方法.成员变量称为静态成员方法.静态成员变量. 优缺点: 优点:使用时不用创建对象,节约了空间.使得代 ...