python学习-property
来源:互联网 发布:北京学软件测试 编辑:程序博客网 时间:2024/06/10 05:39
#!/usr/bin/env python#-*- coding:utf-8 -*-'@property'__author__ = 'hui.qian'class Student(object): passs = Student()s.age = 1000print s.age'''在实际过程中,年龄肯定不会大于1000岁这说明一个问题,属性的设置要有判断检查,不能随便定义,所以一般定义属性都是用set\get方法'''class Student(object): def set_age(self,age): if age > 150: raise ValueError('Shemale!') elif age<0: raise ValueError('The noise?') self.age = age def get_age(self): return self.ages1 = Student()#s1.set_age(-1)#s1.set_age(151)s1.set_age(100)print s1.get_age()'''但是有人就想要用s1.age的方式调用(反正不是我)那就要请@property出场啦'''class Student1(object): @property def age(self): return self._age @age.setter def age(self,value): if value > 150: raise ValueError('Shemale!') elif value<0: raise ValueError('The noise?') else: self._age = value '上面的@property一定要先定义,不然不识别age's2 = Student1()s2.age = 99print s2.age#如果没有setter的话,该属性就变为只读属性
0 0
- python学习笔记 - @property
- python学习笔记 - @property
- python学习-property
- Python模块学习--property
- python学习笔记 - @property
- Python 学习笔记property
- python学习03 --@property
- python学习笔记 使用@property
- python学习——使用@property
- Python学习笔记——Property用法
- python -- property()/@property
- python @property
- Python @property
- Python @property
- Python @property
- python @property
- python Property
- Python @property
- usaco3.3.4亚瑟王的宫殿
- 函数式语言学院派之haskell之一
- quote symbol expected 异常处理
- PHP中使用cURL实现Get和Post请求的方法
- Centos6.5安装OpenLDAP
- python学习-property
- request和response中一些方法的区别
- 【精】适配器模式装饰模式
- hdu1026
- Angular学习(八)——List、Table和其他重复元素
- Drupal7 集成 Solr4.4模块
- hdu 2222 Keywords Search ac自动机模板
- html5 学习之路 二(canvas)
- 最小生成树