python - class propert应用
2024-10-13 03:25:34
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__)
最新文章
- git如何放弃所有本地修改?
- ConsoleApplication 添加对于 System.ServiceModel.Web 引用失败(出现黄色感叹号)的解决办法
- Cheatsheet: 2015 05.01 ~ 05.31
- CentOS进程管理
- 初涉MySQL
- ASP.NET工具
- 【python之旅】python的基础三
- 如何在一个网站或者一个页面,去书写你的JS代码
- The JRE could not be found.Edit the server and change the JRE location.
- QListWidget与QTableWidget的使用以及样式设置
- HDU 4309 Seikimatsu Occult Tonneru 网络流量+像缩进
- ssh常用用法小结
- The Decoder - UVa458
- [转载] Java中动态加载jar文件和class文件
- json的那些事
- PYTHON BS 四大对象
- 异步io模块
- phpMyAdmin 4.7.x CSRF 漏洞利用
- Mac下的开发工具
- POJ-1511 Invitation Cards (单源最短路+逆向)
热门文章
- BZOJ2796[Poi2012]Fibonacci Representation——贪心+二分查找
- BZOJ 2159: Crash 的文明世界(树形dp+第二类斯特林数+组合数)
- [2019/03/17#杭师大ACM]赛后总结(被吊锤记)
- emwin之2D图形绘制问题
- 利用spring boot+vue做的一个博客项目
- Elasticsearch基础知识要点QA
- Raspbian开启root账户
- java -server 和 -client 的不同,及 java -server 时抛错原因
- 程序编译运行和exe运行之文件位置的区别
- P2885 [USACO07NOV]电话线Telephone Wire