@property
2015-09-20 10:55
375 查看
想要理解@property ,必须先理解装饰器的用法
推荐2个讲解装饰器的博文链接:
http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/
0014318435599930270c0381a3b44db991cd6d858064ac0000#0
@property:
作用:可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性。(就是对类对象进行参数检查,但是执行参数检查的函数不显示调用,而是使用@property,直接访问成员变量就隐式调用了参数检查函数)
版本1:
显示进行参数检查:
缺点:调用方法又略显复杂。
使用Python内置的@property 就可以直接将参数检查融入到属性当中,而不必要显示地使用参数检查。
版本2:
总结:因为我们是对score进行参数检查,且假设score是可读可写的,那么就必须设定两个函数:
1、@property {def score(self)} :负责读
2、@score.setter {def score(self,value)}:负责写
@property本身又创建了另一个装饰器@score.setter
并且,需要定义一个私有变量__score
当然,也可以只定义变量只有读属性
如:width,height 有读写属性,resolution只有读属性,因为它没有定义@resolution.setter
但其实,要对类属性等进行参数检查,且要把参数检查的过程隐藏到访问属性时,可以使用魔法函数,__setattr__,__getattr__,而不必使用property。
举例:
推荐2个讲解装饰器的博文链接:
http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/
0014318435599930270c0381a3b44db991cd6d858064ac0000#0
@property:
作用:可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性。(就是对类对象进行参数检查,但是执行参数检查的函数不显示调用,而是使用@property,直接访问成员变量就隐式调用了参数检查函数)
版本1:
显示进行参数检查:
class Student(object): def __init__(self): self.__score = 0 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 s = Student() s.set_score(60) print s.get_score() s.set_score(9999)
缺点:调用方法又略显复杂。
使用Python内置的@property 就可以直接将参数检查融入到属性当中,而不必要显示地使用参数检查。
版本2:
class Student (object): def __init__(self): self.__score = 0 @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 bewteen 0~100!') self.__score = value s = Student() s.score = 60#实际上转化为s.set_score() s.score = 9999#实际上转化为s.get_score()
总结:因为我们是对score进行参数检查,且假设score是可读可写的,那么就必须设定两个函数:
1、@property {def score(self)} :负责读
2、@score.setter {def score(self,value)}:负责写
@property本身又创建了另一个装饰器@score.setter
并且,需要定义一个私有变量__score
当然,也可以只定义变量只有读属性
如:width,height 有读写属性,resolution只有读属性,因为它没有定义@resolution.setter
# -*- coding: utf-8 -*- class Screen(object): def __init__(self): self._height = 0 self._width = 0 @property def width(self): return self._width @width.setter def width(self,value): if not isinstance(value,int): raise ValueError('width must be an interger') self._width = value @property def height(self): return self._height @height.setter def height(self,value): if not isinstance(value,int): raise ValueError('width must be an interger') self._height = value @property def resolution(self): return self._width * self._height # test: s = Screen() s.width = 1024 s.height = 768 print(s.resolution) assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution
但其实,要对类属性等进行参数检查,且要把参数检查的过程隐藏到访问属性时,可以使用魔法函数,__setattr__,__getattr__,而不必使用property。
举例:
class Student (object): def __init__(self): pass def __setattr__(self,name,value): if name == 'score': if not isinstance(value,int): raise ValueError('width must be an interger') if value<0 or value>100: raise ValueError('score must bewteen 0~100!') self.__dict__[name] = value else: raise AttributeError def __getattr__(self,name): if not name == 'score': raise AttributeError return self.__dict__[name] s = Student() s.score = 99 #相当于调用s.__setattr__(score,99) print s.score#相当于调用 s.__getattr__(score)
相关文章推荐
- Linux(Ubuntu) Java(Jdk1.8.0_60)安装以及配置
- 为什么说2015年将是微服务架构元年
- tuzicms企业网站系统安装图文教程
- 推荐几个好玩又有难度的编程网站
- 【linux学习笔记二】常见目录的作用
- linux常用命令(36):free 命令
- 第 九 天 : 复 习 中 ( 二 )
- linux常用命令(35):top 命令
- CentOS 7.1 升级Python2.7.10及编译mod_wsgi指南
- CentOS 7.1 升级Python2.7.10及编译mod_wsgi指南
- 我的Linux学习之路及参考书籍
- 一款综合信息服务后台管理平台OA模板, 适合IE8以上
- 一些值得推荐的网站
- Linux系统LVM基本使用
- centos5.x编译安装apache 手记
- 加密解密、openssl、私有CA创建过程
- LINUX C获取文件大小
- linux源代码 net文件夹
- 解析nginx负载均衡
- Linux sed 命令