Python中的@property装饰器
要了解@property的用途,首先要了解如何创建一个属性。
一般而言,属性都通过__init__方法创建,比如:
class Student(object):
def __init__(self,name,score):
self.name=name
self.score=score
创建实例,运行结果:
tim=Student('Tim',97)
tim.score=100
tim.score
100
mary=Student('Mary',90)
mary.score
90
但是这样子有2个坏处:
1.__init__ 中定义的属性是可变的,如果一个系统的开发人员在知道属性名的情况下,就可以进行随意更改(尽管可能是在无意识的情况下),如果一不小心篡改了,后台排查很难!
2.不利于进行参数检查,比如:score属性范围本该是[0,100],但如果输成了1000也不会报错。
因此,一个标准的创建属性流程如下:
1.定义三个跟属性(本例中是score)相关的函数:
get(用于返回score属性)
set(用于设定score属性)
del(用于删除score属性)
在set函数中,可以添加一些取值范围,比如[0,100].此外,为了私有化属性,前面可以加上__。
这样就做到了既能通过创建实例设定属性,又不让开发人员轻易修改score属性。
如下所示:
class Student(object):
def getScore(self):
return self.__score def setScore(self,score):
if score>100 or score<0:
raise ValueError ('score is out of range.')
else:
self.__score=score def delScore(self):
del self.__score
这样,一旦score取值不在设定范围内,就会报错!
创建一个实例,能够正常运行:
Mary=Student()
Mary.setScore(90)
Mary.getScore()
90
但是,通过方法getScore()查看分数似乎还是有点繁琐,能不能把它当作一个属性去调用呢?至少调用不需要输入()嘛!
当然是可以的。
办法就是通过装饰器:@property
通过给getScore,setScore,delScore三个方法分别添加三个装饰器,就可以直接把这三个方法作为属性去调用了!
如下所示:
class Student(object):
@property
def getScore(self):
return self.__score
@getScore.setter
def setScore(self,score):
if score>100 or score<0:
raise ValueError ('score is out of range.')
else:
self.__score=score
@getScore.deleter
def delScore(self):
del self.__score
tim=Student()
tim.setScore=90
tim.getScore
90
非常神奇!方法居然变成了属性,为什么呢?
因为装饰器@property本质上是一个property()函数,property()函数也是一个装饰器。
一般的装饰器是用在普通函数上,而@property是用在类内的方法上。
property()函数包含了三个部分:getter,setter,deleter。
因为setter和deleter是property()的第二和第三个参数,不能直接套用@语法。
因此,本质上@property相当于getter部分,@setScore.setter相当于setter部分,@delScore.deleter相当于deleter部分。
所以,上面的代码本质上等价于:
class Student(object):
def getScore(self):
return self.__score def setScore(self,score):
if score>100 or score<0:
raise ValueError ('score is out of range.')
else:
self.__score=score def delScore(self):
del self.__score score=property(getScore,setScore,delScore,'description')
最后,为了函数名美观,可以把函数名字改成Score():
class Student(object):
@property
def Score(self):
return self.__score
@Score.setter
def Score(self,score):
if score>100 or score<0:
raise ValueError ('score is out of range.')
else:
self.__score=score
@Score.deleter
def delScore(self):
del self.__score tim=Student()
tim.Score=90
tim.Score
90
几篇个人觉得写的比较清楚的文章:
http://www.runoob.com/python/python-func-property.html
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186781871161bc8d6497004764b398401a401d4cce000
https://www.cnblogs.com/cicaday/p/python-decorator.html
Python中的@property装饰器的更多相关文章
- 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解
第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一. 引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...
- Python中利用函数装饰器实现备忘功能
Python中利用函数装饰器实现备忘功能 这篇文章主要介绍了Python中利用函数装饰器实现备忘功能,同时还降到了利用装饰器来检查函数的递归.确保参数传递的正确,需要的朋友可以参考下 " ...
- python 中多个装饰器的执行顺序
python 中多个装饰器的执行顺序: def wrapper1(f1): print('in wrapper1') def inner1(*args,**kwargs): print('in inn ...
- python中面向对象之装饰器
python面向对象内置装饰器property,staticmethod,classmethod的使用 @property 装饰器作用及使用 作用:面向对象中的方法伪装成属性 使用如下: class ...
- 面向对象中的property装饰器讲解
面向对象中可以用property来修饰我们的函数,必须下面的例子 class Test(object): def __init__(self,name): self.name = name @prop ...
- Python中的各种装饰器详解
Python装饰器,分两部分,一是装饰器本身的定义,一是被装饰器对象的定义. 一.函数式装饰器:装饰器本身是一个函数. 1.装饰函数:被装饰对象是一个函数 [1]装饰器无参数: a.被装饰对象无参数: ...
- 谈谈Python中的decorator装饰器,如何更优雅的重用代码
众所周知,Python本身有很多优雅的语法,让你能用一行代码写出其他语言很多行代码才能做的事情,比如: 最常用的迭代(eg: for i in range(1,10)), 列表生成式(eg: [ x* ...
- Python中的单例模式——装饰器实现剖析
Python中单例模式的实现方法有多种,但在这些方法中属装饰器版本用的广,因为装饰器是基于面向切面编程思想来实现的,具有很高的解耦性和灵活性. 单例模式定义:具有该模式的类只能生成一个实例对象. 先将 ...
- python中闭包和装饰器的理解(关于python中闭包和装饰器解释最好的文章)
转载:http://python.jobbole.com/81683/ 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定python的装饰器,也许因为装饰器确实很难懂.搞定装饰器需 ...
随机推荐
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- Scroller的应用--滑屏实现
1.Scroller源代码分析 以下是对Scroller源代码的分析,并附有源代码.例如以下: package android.widget; import android.content.Conte ...
- 将jsp页面的<s:iterator>的数据返回到action
jsp: <form method="post" id="createTable"> <table width="98%" ...
- coffeescript的上下文
CoffeeScript代码中,变量,甚至函数前面有时会带上一个@符号,那么翻译到 javascript里,就是 "this." 这就涉及到运行过程中的上下文. 这个this指什么 ...
- Mongo性能测试-python脚本
单线程 500+w条数据,插入时间:1小时,13分钟. 脚本: [root@10 hurl]# cat insert-mongo2.py #!/usr/bin/env python #coding=u ...
- DIV+CSS在不同浏览器中的表现
在给员工培训DIV+CSS的过程中.他们向我提出了非常多问题,有些问题我自己也没有想到过于是抽了些时间自己进行了一番实验,所有实验在IE7和Firefox中进行: 实验一.假设一个div没有指定 ...
- adb 命令模拟按键事件 【转】
本文转载自:http://blog.sina.com.cn/s/blog_68f262210102vc1b.html 转自:http://blog.csdn.net/jlminghui/article ...
- mysql创建用户,并授予权限
mysql> GRANT ALL PRIVILEGES ON *.* TO jiqing@"%" IDENTIFIED BY '123456'; Query OK, 0 ro ...
- 【POJ 1716】 Integer Intervals
[题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ...
- istio-禁用/允许sidecar设置
一.在namespace设置自动注入: 给 default 命名空间设置标签:istio-injection=enabled: $ kubectl label namespace default is ...