码迷,mamicode.com
首页 > 其他好文 > 详细

7_28 装饰器 9+21+20 需要重新写一次

时间:2018-07-28 12:04:36      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:key   ===   init   cte   ini   type   typeerror   ted   Owner   

v

class Typed:
def __init__(self,key,expected_type):
self.key=key
self.expected_type=expected_type
def __get__(self, instance, owner):
print(‘get方法‘)
# print(‘instance参数【%s】‘ %instance)
# print(‘owner参数【%s】‘ %owner)
return instance.__dict__[self.key]
def __set__(self, instance, value):
print(‘set方法‘)
# print(‘instance参数【%s】‘ % instance)
# print(‘value参数【%s】‘ % value)
# print(‘====>‘,self)
if not isinstance(value,self.expected_type):
# print(‘你传入的类型不是字符串,错误‘)
# return
raise TypeError(‘%s 传入的类型不是%s‘ %(self.key,self.expected_type))
instance.__dict__[self.key]=value
def __delete__(self, instance):
print(‘delete方法‘)
# print(‘instance参数【%s】‘ % instance)
instance.__dict__.pop(self.key)

class People:
name=Typed(‘name‘,str) #t1.__set__() self.__set__()
age=Typed(‘age‘,int) #t1.__set__() self.__set__()
def __init__(self,name,age,salary):
self.name=name
self.age=age
self.salary=salary

# p1=People(‘alex‘,‘13‘,13.3)
p1=People(213,13,13.3)

# p1=People(‘alex‘,13,13.3)
# print(p1.__dict__)
# p1=People(213,13,13.3)
# print(p1.__dict__)
# print(p1.__dict__)
# print(p1.name)

# print(p1.__dict__)
# p1.name=‘egon‘
# print(p1.__dict__)


# print(p1.__dict__)
# del p1.name
# print(p1.__dict__)

# print(p1)

# print(p1.name)
# p1.name=‘egon‘
# print(p1.name)
# print(p1.__dict__)



7_28 装饰器 9+21+20 需要重新写一次

标签:key   ===   init   cte   ini   type   typeerror   ted   Owner   

原文地址:https://www.cnblogs.com/yikedashuyikexiaocao/p/9380494.html

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