内置装饰器二:@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 ...
随机推荐
- PythonQt在windows下的编译
笔者最近在做Qt方面的开发工作,然后需要用到脚本程序对程序内部进行扩展,就很自然的想到了Python,度娘一下发现了一款神器,也就是今天给大家介绍的主角:PythonQt 今天首先给大家介绍下Pyth ...
- ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- cuda使用
import torchx = torch.randn(2, 3)x = x.cuda() print (x)#最简单的cuda使用,但是感觉好慢啊
- 2018.10.02 NOIP模拟 序列维护(线段树+广义欧拉定理)
传送门 一道比较好的线段树. 考试时线性筛打错了于是弃疗. 60分暴力中有20分的快速幂乘爆了于是最后40分滚粗. 正解并不难想. 每次区间加打懒标记就行了. 区间查询要用到广义欧拉定理. 我们会发现 ...
- gj11 多线程、多进程和线程池编程
11.1 python中的GIL # coding=utf-8 # gil global interpreter lock (cpython) # python中一个线程对应于c语言中的一个线程 # ...
- UVa 11178 Morley's Theorem (几何问题)
题意:给定三角形的三个点,让你求它每个角的三等分线所交的顶点. 析:根据自己的以前的数学知识,应该很容易想到思想,比如D点,就是应该求直线BD和CD的交点, 以前还得自己算,现在计算机帮你算,更方便, ...
- mysql中要根据某个逗号分割的字符串关联查询另一张表的数据
首先观察下面的查询 select * from company where f_id in ('210','205','208') select * from company where f_id i ...
- python技巧31[python中使用enum][转]
以下几种方法来模拟enum:(感觉方法一简单实用) # way1 class Directions: up = 0 down = 1 left = 2 right =3 ...
- (KMP Next的运用) Period II -- fzu -- 1901
http://acm.fzu.edu.cn/problem.php?pid=1901 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=703 ...
- no_namespace rename 在C++中是什么意思啊
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("E ...