Python @property装饰器
来源:互联网 发布:手机淘宝占用空间大 编辑:程序博客网 时间:2024/05/22 04:37
廖老师的博客链接如下,一开始没看懂,搜罗了一大堆,有点感觉了
点击打开链接
其实@property装饰器就是把class的方法变成属性,见下面这个class,它有两个私有属性。
通过第一个@property和第二个@score.setter,我们可以像访问属性一样来调用类里面的方法,例如:
s=Student("David",99)
s.score = 100
至于用这种装饰器的原因,我想,就是为了简洁吧,直接用属性赋值的方式,执行了方法。Python 肯定是个懒人发明的。
class Student(object): def __init__(self,name,score): self.__name = name self.__score = score @property def score(self): return self.__score @score.setter def score(self,value): if not isinstance(value,int): raise ValueError("invalid score!!!") elif value < 0 or value > 100: raise ValueError("score must be between [0,100]!!!") else: self.__score = value @property def name(self): return self.__name
0 0
- Python @property装饰器
- python装饰器--@property
- Python装饰器之 property()
- python中的@property装饰器
- python装饰器之staticmethod,classmethod,property
- python装饰器之staticmethod,classmethod,property
- python中@property装饰器的用法
- python装饰器之staticmethod,classmethod,property
- Python 装饰器,@property 以及 Pycaffe.py
- Python 装饰器,@property 以及 Pycaffe.py
- python中@property装饰器的用法
- Python中的property, setter装饰器
- Python 内置装饰器staticmethod、classmethod、property
- python内部装饰器@classmethod, @staticmethod, @property
- 内置装饰器@property
- @property 装饰器
- @property装饰器
- Python:使用property装饰器将方法转为属性
- CSS入门教程——定位(positon)
- SQL优化
- mysql 数据类型
- mysql 常用命令
- UBUNTU 下GIT的安装
- Python @property装饰器
- Linux 删除文件夹和文件的命令
- linux下git使用记录1 git 提交
- Linux命令之nano -
- 用gitolite新建项目,clone后首次push,可能会出现: git: No refs in common and none specified; doing no
- 设置 git config 的一些默认配置
- git config命令使用
- git config命令使用
- Linux :: vi E212: Can't open file for writing