Python面向对象-@property装饰器
python中,我们可以直接添加和修改属性的值:
>>> class Student(object):
... pass
...
>>> s = Student()
>>> s.score = 101
但是,101明显超过了满分100分,不合理。可以通过创建setScore()方法,进行参数检查:
>>> class Student(object):
... def setScore(self,score):
... if not isinstance(score,int):
... raise ValueError('分数必须是一个整数')
... if score < 0 or score > 100:
... raise ValueError('分数值必须在0到100之间')
... self.score = score
... def getScore(self):
... return self.score
...
>>> s = Student()
>>> s.setScore(200)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in setScore
ValueError: 分数值必须在0到100之间
>>> s.setScore(99)
>>> s.getScore()
99
但是上面的过程略显复杂。
这时,可以使用@property,既可以用类似属性的方法访问类的变量,又可以检查参数,@property实现起来稍微有点复杂:
>>> class Student(object):
... @property
... def score(self):
... return self._score
... @score.setter
... def score(self,score):
... if not isinstance(score,int):
... raise ValueError('必须是整数')
... if score < 0 or score > 100:
... raise ValueError('必须在0到100之间')
... self._score = score
...
>>> s = Student()
>>> s.score = 88
>>> s.score = 55
>>> s.score = 101
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in score
ValueError: 必须在0到100之间
把一个getter方法变成属性,只需要加上@property即可,同时,@property本身又创建了另外一个装饰器@score.setter,该装饰器负责把一个setter方法变成属性赋值。
另外,只定义getter方法,不定义setter方法就是在定义一个只读属性:
>>> class Student(object):
... @property
... def birth(self):
... return self._birth
... @birth.setter
... def birth(self,birth):
... self._birth = birth
... @property
... def age(self):
... return 2018 - self._birth
...
>>> s =Student()
>>> s.birth = 1991
>>> s.birth
1991
>>> s.age
27
>>> s.age = 20
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
最后一步,证明了age只是一个只读属性!
Python面向对象-@property装饰器的更多相关文章
- Python的property装饰器的基本用法
Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...
- python中@property装饰器的使用
目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...
- [转载]Python使用@property装饰器--getter和setter方法变成属性
原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...
- 【python】@property装饰器
Python内置的@property装饰器可以把类的方法伪装成属性调用的方式.也就是本来是Foo.func()的调用方法,变成Foo.func的方式.在很多场合下,这是一种非常有用的机制. class ...
- Python 利用@property装饰器和property()方法将一个方法变成属性调用
在创建实例属性时,如果直接把实例属性暴露出去,虽然写起来简单,但是存在一些风险,比如实例属性可以在外部被修改. 为了限制外部操作,可以通过一个set_score()方法来设置成绩,再通过一个get_s ...
- Python之property装饰器
参考: http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035600.html http://joy2everyone.iteye.com/ ...
- python面向对象:组合、封装、property装饰器、多态
一.组合二.封装三.property装饰器四.多态 一.组合 ''' 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. ...
- 面向对象之封装 及@property装饰器使用
目录 封装 1.封装的定义 2.封装的目的: 3.封装的三种方式 4.封装的优点 5.访问限制(封装) @property 装饰器 属性property底层实现 封装 1.封装的定义 将复杂的丑陋的, ...
- python中面向对象之装饰器
python面向对象内置装饰器property,staticmethod,classmethod的使用 @property 装饰器作用及使用 作用:面向对象中的方法伪装成属性 使用如下: class ...
随机推荐
- postgresql12 b-tree v4空间上和性能上的优化
在 pg v11 和 v12 上 常见测试用例 CREATE TABLE rel ( a bigint NOT NULL, b bigint NOT NULL ); ALTER TABLE rel A ...
- 运用python实现冒泡排序算法
冒泡排序,一个经典的排序算法,因在算法运行中,极值会像水底的气泡一样逐渐冒出来,因此而得名. 冒泡排序的过程是比较两个相邻元素的大小,然后根据大小交换位置,这样从列表左端开始冒泡,最后最大值会依次从右 ...
- 常见 MIME 类型列表(转)
本文转自:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MI ...
- 音视频入门-14-JPEG文件格式详解
* 音视频入门文章目录 * JPEG 文件格式解析 JPEG 文件使用的数据存储方式有多种.最常用的格式称为 JPEG 文件交换格式(JPEG File Interchange Format,JFIF ...
- 使用selenium模拟登陆新浪微博
1.selenium基本使用 1.selenium安装及基本操作 selenium是一个自动化测试工具,它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面浏览器驱动,也包括Ph ...
- Linux的curl和wget
wget wget命令用来从指定的URL下载文件.wget非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性,如果是由于网络的原因下载失败,wget会不断的尝试,直到整个文件下载完毕.如果是服 ...
- 【前端】之jQuery基础知识
jQuery 简介 在项目中引入jQuery: 去jQuery官网下载jQuery包:jquery-3.2.1.min.js 将下载的jQuery包添加到项目目录中 在标签下添加jQuery引用:&l ...
- U盘安装centos 7 提示 “Warning: /dev/root does not exist
背景介绍:公司需要使用台式机安装Centos 7.5 系统,来部署一个测试的数据库,在安装Centos 7.5 系统的时候,使用U启安装,但有问题. 提示信息如下 如图:安装centos 7时提示 & ...
- 1、Docker 简介
目录 Docker 起源 Docker 架构 特性 局限 名称空间隔离 原理 Control Groups (cgroups) Docker Docker 啥是docker? Docker 是一个开源 ...
- javascript获取当前时间CurentTime
function CurentTime(){ var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMo ...