码迷,mamicode.com
首页 > 编程语言 > 详细

python之描述符的应用

时间:2020-02-23 18:41:58      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:get   sof   mil   ict   erro   style   pre   foo   ros   

class Foo:
def __init__(self,key,hope_type):
self.key=key
self.hope_type=hope_type
def __get__(self, instance, owner):
print(‘输出get‘)
return instance.__dict__[self.key]
def __delete__(self, instance):
print(‘输出delete‘)
instance.__dict__.pop(self.key)
def __set__(self, instance, value):
if not isinstance(value,self.hope_type):
raise TypeError(‘您输入的类型是%s‘%self.hope_type)
instance.__dict__[self.key]=value


class Test:
name=Foo(‘name‘,str)
age=Foo(‘age‘,int)
def __init__(self,name,age,salary):
self.name=name
self.age=age
self.salary=salary
f=Test(‘alex‘,‘18‘,200)

python之描述符的应用

标签:get   sof   mil   ict   erro   style   pre   foo   ros   

原文地址:https://www.cnblogs.com/cxydnxs/p/12350716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!