propert应用

#方式一:
# class Student(object):
# def __init__(self,name,sc):
# self.name = name
# self.sc = sc
#
# @property
# def score(self):
# return self.sc
#
# @score.setter
# def score(self,value):
# if not isinstance(value, int):
# raise ValueError('分数必须是整数才行呐')
# if value < 0 or value > 100:
# raise ValueError('分数必须0-100之间')
# self.sc = value
#
# @score.deleter
# def score(self):
# del self.sc #----------------------------------------------------------- #方式二:
class Student(object):
def __init__(self,name,sc):
self.name = name
self.sc = sc def get_score(self):
return self.sc def set_score(self,value):
if not isinstance(value, int):
raise ValueError('分数必须是整数才行呐')
if value < 0 or value > 100:
raise ValueError('分数必须0-100之间')
self.sc = value def del_score(self):
del self.sc score = property(get_score,set_score,del_score) X = Student('anec',50)
print(X.__dict__)
print(X.score)
X.score = 90
print(X.score)
del X.score
print(X.__dict__)

最新文章

  1. git如何放弃所有本地修改?
  2. ConsoleApplication 添加对于 System.ServiceModel.Web 引用失败(出现黄色感叹号)的解决办法
  3. Cheatsheet: 2015 05.01 ~ 05.31
  4. CentOS进程管理
  5. 初涉MySQL
  6. ASP.NET工具
  7. 【python之旅】python的基础三
  8. 如何在一个网站或者一个页面,去书写你的JS代码
  9. The JRE could not be found.Edit the server and change the JRE location.
  10. QListWidget与QTableWidget的使用以及样式设置
  11. HDU 4309 Seikimatsu Occult Tonneru 网络流量+像缩进
  12. ssh常用用法小结
  13. The Decoder - UVa458
  14. [转载] Java中动态加载jar文件和class文件
  15. json的那些事
  16. PYTHON BS 四大对象
  17. 异步io模块
  18. phpMyAdmin 4.7.x CSRF 漏洞利用
  19. Mac下的开发工具
  20. POJ-1511 Invitation Cards (单源最短路+逆向)

热门文章

  1. BZOJ2796[Poi2012]Fibonacci Representation——贪心+二分查找
  2. BZOJ 2159: Crash 的文明世界(树形dp+第二类斯特林数+组合数)
  3. [2019/03/17#杭师大ACM]赛后总结(被吊锤记)
  4. emwin之2D图形绘制问题
  5. 利用spring boot+vue做的一个博客项目
  6. Elasticsearch基础知识要点QA
  7. Raspbian开启root账户
  8. java -server 和 -client 的不同,及 java -server 时抛错原因
  9. 程序编译运行和exe运行之文件位置的区别
  10. P2885 [USACO07NOV]电话线Telephone Wire