#描述符可以实现大部分python类特性中的底层魔法,包括,@classmethod @staticmethod @property甚至是__slots__
# class Typed:
# def __init__(self,key,expected_type):
# self.key=key
# self.expected_type=expected_type
# def __get__(self, instance, owner):
# print('get_method...')
# # print('instance参数[%s]' %instance)
# # print('owner参数[%s]' %owner)
# return instance.__dict__[self.key]
# def __set__(self, instance, value):
# print('set_method...')
# # print('instance参数[%s]' %instance)
# # print('value的参数[%s]' %value)
# # print(instance.__dict__)
# 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_method...')
# # print('instance参数[%s]' %instance)
# instance.__dict__.pop(self.key)
#
# class People:
# name=Typed('name',str)
# age=Typed('age',int)
# def __init__(self,name,age,salary):
# self.name=name
# self.age=age
# self.salary=salary
# # p1=People('jack',19,39.4)
# # p1=People(3454,1522,39.4) #传入错误的参数时
#
# print(p1.__dict__)
# # print(p1.name)
# #
# # p1.name='milk'
# # print(p1.__dict__)
# # del p1.name
# # print(p1.__dict__)