在绑定属性时,如果我们直接把属性暴露出去,虽然写起来简单,但是,没有办法检查参数,导致可以把成绩随便改

>>> class Student(object):
... pass
...
>>> s=Student()
>>> s.score=999
>>> s.score
999
>>> s.score='abc'
>>> s.score
'abc'

  这显然不会逻辑,为了现在score的范围可以,通过一个set_score()方法,同通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数:

class Student(object):
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

  现在,对人员的Student实例进行操,就不能随心所欲地设置score了

>>> s.set_score(100)
>>> s.get_score()
100
>>> s.set_score(101)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in set_score
ValueError: score must between 0~100!

  但是,上面的调用方法比较复杂,有没有直接用属性这么直接减掉

  有没有既能检查参数,又可以有类似属性这样的简单方式来访问类的变量呢。对于类的方法,装饰器一样起作用。python内置的@property装饰器就是负责把一个方法变成属性调用的:

class Student(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

  @property的实现比较复杂,这里看如何使用。把一个getter的方法变成属性,只需要加上@property就可以了,此时,@property本身又创建了另一个装饰器,负责把一个setter方法变成属性赋值,于是我们就拥有了一个可控的属性操作:

>>> s=Student()
>>> s.score=100
>>> s.score
100
>>> s.score=101
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 18, in score
ValueError: score must between 0~100!

  注意到这个神奇的@property,我们再对实例属性操作的时候,就知道该属性很可能不是直接暴露的,而是通过getter和setter方法来实现的。

  还可以只定义只读属性,只定义getter方法,不定义setter方法就是一个只读属性

class Student(object):
#出生年格式为2001
@property
def birth(self):
return self._birth
@birth.setter
def birth(self,value):
self._birth=value
#年龄age定义为只读即没有setter属性
@property
def age(self):
return 2019-self._birth

  操作

>>> s=Student()
>>> s.birth=2000
#age是根据birth用2019-birth计算出来的
>>> s.age
19
#直接修改age因为没有定义setter使用报错
>>> s.age=20
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute

  练习

  利用@property给一个Screen对手加上width和heigth属性,已经一个只读属性resolution  Screen.py

class Screen(object):
#设置长
@property
def width(self):
return self._width
#检查输入是否规范必须是int并且大于0
@width.setter
def width(self,value):
if not isinstance(value,int):
raise ValueError('width must be an integer!')
if value < 0:
raise ValueError('width must > 0')
self._width=value
#设置高
@property
def height(self):
return self._height
@height.setter
def height(self,value):
if not isinstance(value,int):
raise ValueError('height must be an integer!')
if value < 0:
raise ValueError('height must > 0')
self._height=value
#面积只设置getter方法通过长乘高得到
@property
def resolution(self):
return self._width*self._height s=Screen()
s.width=1024
s.height=768
print(s.resolution)

  

Python3之使用@property的更多相关文章

  1. python3 封装之property 多态 绑定方法classmethod 与 非绑定方法 staticmethod

    property 特性 什么是特性property property 是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值 例如 BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而 ...

  2. python3与django中@property详解

    django提供了内置装饰器 @staticmethod\@classmethod\property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property ...

  3. python3 - property的使用

    传统的绑定属性值,会把属性暴露出去,而且无法检查参数是否合法,如下: class Test(object): def  __int__(self,age): self.age = age 为了检查参数 ...

  4. python3 高级编程(三) 使用@property

    @property装饰器就是负责把一个方法变成属性调用的. @property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 cl ...

  5. python3 速查参考- python基础 8 -> 面向对象基础:类的创建与基础使用,类属性,property、类方法、静态方法、常用知识点概念(封装、继承等等见下一章)

    基础概念 1.速查笔记: #-- 最普通的类 class C1(C2, C3): spam = 42 # 数据属性 def __init__(self, name): # 函数属性:构造函数 self ...

  6. Python3.5-20190519-廖老师-自我笔记-面向对象中slots变量--@property的使用

    python是动态语言,可以随时随地给实例对象添加属性和方法,但是我们想限制属性的名字,可以使用__slots__特殊变量来限制 使用__slots__要注意,__slots__定义的属性仅对当前类实 ...

  7. python3(二十七)property

    """ """ __author__ = 'shaozhiqi' # 绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单, # 但是, ...

  8. python3 的setter方法及property修饰

    #!/usr/bin/env pthon#coding:utf-8 class person(object): def __init__(self,name,sex,age,surface,heigh ...

  9. 也说python的类--基于python3.5

    在面向对象的语言中,除了方法.对象,剩下的一大重点就是类了,从意义上来讲,类就是对具有相同行为对象的归纳.当一个或多个对象有相同属性.方法等共同特征的时候,我们就可以把它归纳到同一个类当中.在使用上来 ...

随机推荐

  1. LevelDB的源码阅读(三) Get操作

    在Linux上leveldb的安装和使用中我们写了这么一段测试代码,内容以及输出结果如下: #include <iostream> #include <string> #inc ...

  2. vue2 单一事件中心管理组件通信

  3. 54、servlet3.0-ServletContainerInitializer

    54.servlet3.0-ServletContainerInitializer Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.S ...

  4. bash: sz: command not found

    Linux系统中如果没有安装 lrzsz这个包,就会报rz.sz命令找不到,安装即可解决. 命令: yum install lrzsz 效果图:

  5. Linux 硬件软件时间同步

    同步BIOS时钟,强制把系统时间写入CMOS clock --show   查看硬件时间clock -w       强制把系统时间写入CMOSclock --show   查看硬件时间reboot ...

  6. 数据库删除数据 truncate 与 delete

    delete from table where 直接删除表中的某一行数据,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作.所以delete相比较truncate更加占用资源,数据 ...

  7. @babel/preset-env useBuiltIns 说明

    推荐阅读:https://blog.hhking.cn/2019/04/02/babel-v7-update/ useBuiltIns false 1 "useBuiltIns": ...

  8. codeforces164A

    Variable, or There and Back Again CodeForces - 164A Life is not easy for the perfectly common variab ...

  9. ros python 四元数 转 欧拉角

    import sysimport math w = -0.99114048481x = -0.00530699081719y = 0.00178255140781z = -0.133612662554 ...

  10. AGC022E Median Replace

    题意 给出一个长度为奇数\(n\)的残缺01串,问有多少种补全方法,每次将连续三个位替换为它们的中位数后,能有一种方案使它变为1. \(n \le 3*10^5\) 思路 左边表示栈顶. 将操作简化为 ...