python 25 类装饰器
# python 类装饰器 # /usr/sbin/py/python # -*=coding:utf8-*- def dec(obj): obj.hobby = "singing" # 添加类属性 return obj # 加任意参数 def type(**kwargs): def deco(obj): for key,val in kwargs.items(): # obj.key = val setattr(obj,key,val) return obj return deco @type(school="实验小学",address="上海") # 分析 type方法返回值是deco方法 等价于 @deco 》》 Student = deco(Student) class Student: pass print(Student.__dict__) # 利用装饰器 实现参数类型限制 class Type: def __init__(self,key,parameterType): self.key = key self.parameterType = parameterType def __get__(self, instance, owner): return instance.__dict__[self.key] def __set__(self, instance,