python __set__ __get__ __delete__
2024-08-25 04:04:26
class Attr(object):
def __init__(self,attrname,attrtype):
self.attrname=attrname
self.attrtype=attrtype
def __get__(self,instance,value):
return instance.__dict__[self.attrname]
def __set__(self,instance,value):
if not isinstance(value,self.attrtype):
raise TypeError("%s type error"%self.attrname)
instance.__dict__[self.attrname]=value
def __delete__(self,instance):
del instance.__dict__[self.attrname] class Person(object):
name=Attr("name",str)
age=Attr("age",int)
p=Person()
p.age=""
最新文章
- Format
- C/C++ 中判断某一文件或目录是否存在
- 页面Button/Link 传参数
- .net MVC 连接数据本地数据库三种方法
- [转]Oracle 分组聚合二种写法,listagg和wmsys.wm_concat
- 剑指offer系列26--正则表达式匹配
- 2434: [Noi2011]阿狸的打字机
- openstack theme topic
- ssh框架搭建时报错
- 深入理解viewport(转)
- c中const定义的问题
- 迁移git
- ORACLE获取SQL绑定变量值的方法总结
- 剑指offer编程题Java实现——面试题13在O(1)时间内删除链表节点
- VUE2 第五天学习--过渡效果
- Netty入门(2) - 核心概念
- caffe在solverstate的基础上继续训练模型
- MPT树详解
- Python中让MySQL查询结果返回字典类型的方法
- thinkphp AOP(面向切面编程)