@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
1》只有@property表示只读。
2》同时有@property和@x.setter表示可读可写。

3》同时有@property和@x.setter和@x.deleter表示可读可写可删除。

  1. class student(object):  #新式类
  2. def __init__(self,id):
  3. self.__id=id
  4. @property  #读
  5. def score(self):
  6. return self._score
  7. @score.setter #写
  8. def score(self,value):
  9. if not isinstance(value,int):
  10. raise ValueError('score must be an integer!')
  11. if value<0 or value>100:
  12. raise ValueError('score must between 0 and 100')
  13. self._score=value
  14. @property #读(只能读,不能写)
  15. def get_id(self):
  16. return self.__id
  17. s=student('123456')
  18. s.score=60 #写
  19. print s.score #读
  20. #s.score=-2 #ValueError: score must between 0 and 100
  21. #s.score=32.6 #ValueError: score must be an integer!
  22. s.score=100 #写
  23. print s.score #读
  24. print s.get_id #读(只能读,不可写)
  25. #s.get_id=456 #只能读,不可写:AttributeError: can't set attribute

运行结果:
60
100
123456

@property和@x.setter和@x.deleter表示可读可写可删除的更多相关文章

  1. property与x.setter与x.deleter

    #测试property,x.setter,x.deleter class Test1: def __init__(self): self.__private = "alex" @p ...

  2. @property与@xxx.setter的用法

    类中@property与@xxx.setter的方法介绍. 简单说,@property就是将定义的函数(方法)当作属性对象使用,不需要像调用函数那样去调用,而@xxx.setter是为@xxx的这样函 ...

  3. property内置装饰器函数和@name.setter、@name.deleter

    # property # 内置装饰器函数 只在面向对象中使用 # 装饰后效果:将类的方法伪装成属性 # 被property装饰后的方法,不能带除了self外的任何参数 from math import ...

  4. @property和@score.setter的用法

    @property 把属性装饰成get方法 给属性赋值时,会自动调用@property装饰的方法 只设置属性的@property 时,属性为只读 @score.setter 把属性装饰成set方法 给 ...

  5. python使用@property @x.setter @x.deleter

    @property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的. 1>只有@property表示只读. 2>同时 ...

  6. python 全栈开发,Day22(封装,property,classmethod,staticmethod)

    一.封装 封装 : 广义上的 :把一堆东西装在一个容器里 狭义上的 :会对一种现象起一个专门属于它的名字 函数和属性装到了一个非全局的命名空间 —— 封装 隐藏对象的属性和实现细节,仅对外提供公共访问 ...

  7. 内置装饰器二:@property

    property 装饰器的作用 property 装饰器将方法包装成属性,将私有属性公有化,此属性只能被读取.相当于实现get方法的对象 class People: def __init__(self ...

  8. python基础===装饰器@property 的扩展

    以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a proper ...

  9. [Python] 08 - Classes --> Objects

    故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...

随机推荐

  1. linux和shell关系

    坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/   索引: 什么是shell shell的分类 shell脚本的执行方式   ...

  2. C++的Json解析库:jsoncpp和boost(转)

    原文转自 http://blog.csdn.net/hzyong_c/article/details/7163589 JSON(JavaScript Object Notation)跟xml一样也是一 ...

  3. OPEN SUSE LINUX

    1. 把中文界面变成英文界面 yast2->system->language: 主要语言: 美式英语US 2. 使用root用户默认登录 Ubuntu使用root登录        Ubu ...

  4. 设计模式-python实现

    设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...

  5. 第一章:1-11、在上题的分组交换网中,设报文长度和分组长度分别为x和(p+h)(bit),其中p为分组的数据部分的长度,而h为每个分组所带的控制信息固定长度,与p的大小无关。通信的两端共经过k段链路。链路的数据率为b(bit/s),但传播时延和结点的排队时间均可忽略不计。若打算使总的时延为最小,问分组的数据部分长度p应取为多大?

    <计算机网络>谢希仁著第四版课后习题答案答: 分组个x/p, 传输的总比特数:(p+h)x/p 源发送时延:(p+h)x/pb 最后一个分组经过k-1个分组交换机的转发,中间发送时延:(k ...

  6. 模拟【P1650】 田忌赛马

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述--->p1650 田忌赛马 先%dalao sto GMPotlc orz 他教给的我,征求意见后终于来水题解. 分析 我们需要知 ...

  7. Strobogrammatic Number II -- LeetCode

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  8. 远程debug---远程服务器参数设置

    java  -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=25 -jar  myboot.jar 或者  java ...

  9. (转)unity3d加密资源并缓存加载

    http://www.haogongju.net/art/1931680 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止依然没有更新正确的示例代码. view source   pr ...

  10. 数值计算方法 | C语言实现几个数值计算方法(实验报告版)

    目录 写在前面 实验一 牛顿插值方法的实现 实验二 龙贝格求积算法的实现 实验三 高斯列主元消去法的实现 实验四 最小二乘方法的实现 写在前面 使用教材:<数值计算方法>黄云清等编著 科学 ...