在绑定属性时,如果我们将属性直接暴露在外面,就可能导致属性被任意修改,有时候这个是我们不希望看到的
如:设置学生的成绩
class Student(object):
def __init__(self):
self.score = 0
#这个显然不符合属性的规范
#std = Student()
#std.score = 99999
#print std.score
#于是我们采用内部限制的方法来设定
 class Student2(object):
def __init__(self):
pass def get_score(self):
return self._score def set_score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value std2 = Student2()
std2.set_score(60)
print std2.get_score() # std2.set_score(99999)
print std2.get_score() Traceback (most recent call last):
File "/home/mywork/oldboy/practice/property.py", line 44, in <module>
std2.set_score(99999)
File "/home/mywork/oldboy/practice/property.py", line 35, in set_score
raise ValueError('score must between 0 ~ 100!')
ValueError: score must between 0 ~ 100!
#但是,上面的调用方法又略显复杂,这就是需要应用@property
#他的本质就是把一个getter方法变成属性
#此时,@property本身又创建了另一个装饰器@score.setter,
#负责把一个setter方法变成属性赋值,于是,我们就拥有一个可控的属性操作:
看代码:
 class Student3(object):

     @property
def score(self):
return self._score @score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value std3 = Student3()
std3.score = 90
print std3.score #
std3.score = 9000 #直接设置时就报错 Traceback (most recent call last):
File "/home/mywork/oldboy/practice/property.py", line 69, in <module>
std3.score = 9000
File "/home/mywork/oldboy/practice/property.py", line 63, in score
raise ValueError('score must between 0 ~ 100!')
ValueError: score must between 0 ~ 100!

python中@property的使用的更多相关文章

  1. python 中 property 属性的讲解及应用

    Python中property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回 property属性的有两种方式: 1. 装饰器 即:在方法上应用装饰器 2. 类属性 即 ...

  2. python 中property函数如何实现

    实际上,在python中property(fget,fset,fdel,doc)函数不是一个真正的函数,他其实是拥有很多特殊方法的类. 这特殊类总的很多方法完成了property函数中的所有工作,涉及 ...

  3. python中@property装饰器的使用

    目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...

  4. 转载-对于Python中@property的理解和使用

    原文链接:https://blog.csdn.net/u013205877/article/details/77804137 重看狗书,看到对User表定义的时候有下面两行 @property def ...

  5. python中property属性的介绍及其应用

    Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回. 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的 ...

  6. 对于Python中@property的理解和使用

    @property 这个我们在很多代码中都会用到,简单讲即为一个只读属性的调用 如果需要有修改权限,需要再加一个@属性名.setter 例: #!/usr/bin/env python # -*- c ...

  7. python 中@property的使用

    从14年下半年开始接触到python,自学了一段时间,后又跟别人学习了下,把基础知识基本上学过了.忽然感觉python不可能这么简单吧,就这么点东西?后来看了下书,发现还有很多的高级部分.连续看了两天 ...

  8. Python 中@property的用法

    在绑定属性时,如果我们直接把属性赋值给对象,比如: p = Person() p.name= 'Mary' 我们先看个详细的例子(注意双下划线name和age定义为私有变量): class Perso ...

  9. python中 @property

    考察 Student 类: class Student(object): def __init__(self, name, score): self.name = name self.score = ...

  10. Python中property属性的概论和使用方法

    property属性 概念: 定义一个方法但是使用装饰器property,只可以有一个self形参 可以用这样的属性动态的获取属性的值 定义方式(经典类) class Fun(): @property ...

随机推荐

  1. javascript作用域链理解

    执行上下文(Execution context,简称EC)   概念   每当控制器到达ECMAScript可执行代码的时候,就进入了一个执行上下文.   javascript中,EC分为三种:   ...

  2. bzoj1093【ZJOI2007】最大半联通子图

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1093 sol  :一开始理解错题意了QAQ,还莫名其妙写挂了QAQ,调了半天 首先显然一个强联 ...

  3. 调试Java代码(Eclipse)汇总

    Java 10个调试技巧(基础❤❤❤❤❤) Eclipse断点调试(和上一篇基本类似,补充❤❤) 使用Eclipse开发和调试java程序(从安装eclipse开始,特别细,有设置条件断点,回退的具体 ...

  4. cmake 版本升级

    1.在网址 https://cmake.org/files/v3.1/下载   cmake-3.1.0.tar.gz 2.解压 3.执行  ./configure 4.执行 make 5. 执行   ...

  5. 控制cxGrid 主从表的明细只展开一个

    procedure TForm.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIn ...

  6. SQL2005、SQL2008如何压缩日志文件(log) 如何清除日志

    原文发布时间为:2010-11-01 -- 来源于本人的百度文章 [由搬家工具导入]            ALTER DATABASE [DataBaseName]             SET ...

  7. jquery text

    scenario: need to display raw xml, what does text() method do: <> converted to <> i.e. 把 ...

  8. angular 右击事件的写法

    .directive('ngRightClick', function ($parse){ return function (scope, element, attrs){ var fn = $par ...

  9. 图表插件echars的使用案例

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  10. hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp

    题目链接 题意 给定一个\(N\)个点的无向图,求从任意一个点出发,经过所有点的最短路径长度(每个点至多可以经过两次). 思路 状态表示.转移及大体思路 与 poj 3311 Hie with the ...