zoukankan      html  css  js  c++  java
  • 类的装饰器

    类的装饰器

    # def deco(func):
    #     print('==========')
    #     return func
    #
    # # @deco       #test=deco(test)
    # # def test():
    # #     print('test函数运行')
    # # test()
    #
    # @deco #Foo=deco(Foo)
    # class Foo:
    #     pass
    
    
    
    
    def deco(obj):
        print('==========',obj)
        obj.x=1
        obj.y=2
        obj.z=3
        return obj
    # @deco #Foo=deco(Foo)
    # class Foo:
    #     pass
    #
    # print(Foo.__dict__)
    
    #一切皆对象
    # # @deco #test=deco(test)
    # def test():
    #     print('test函数')
    # test.x=1
    # test.y=1
    # print(test.__dict__)

     修订版

    def Typed(**kwargs):
        def deco(obj):
            for key,val in kwargs.items():
                # obj.key=val
                setattr(obj,key,val)
            return obj
        return deco
    
    @Typed(x=1,y=2,z=3)   #1.Typed(x=1,y=2,z=3) --->deco   2.@deco---->Foo=deco(Foo)
    class Foo:
        pass
    print(Foo.__dict__)
    
    # @Typed(name='egon')  #@deco   ---->Bar=deco(Bar)
    # class Bar:
    #     pass
    # print(Bar.name)

     类装饰器的应用

    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)
    
    def deco(**kwargs): #kwargs={'name':str,'age':int}
        def wrapper(obj): #obj=People
            for key,val in kwargs.items():#(('name',str),('age',int))
                setattr(obj,key,Typed(key,val))
                # setattr(People,'name',Typed('name',str)) #People.name=Typed('name',str)
            return obj
        return wrapper
    @deco(name=str,age=int)  #@wrapper ===>People=wrapper(People)
    class People:
        name='alex'
        # name=Typed('name',str)
        # age=Typed('age',int)
        def __init__(self,name,age,salary,gender,heigth):
            self.name=name
            self.age=age
            self.salary=salary
    # p1=People('213',13.3,13.3,'x','y')
    print(People.__dict__)
  • 相关阅读:
    《统计学习方法》笔记十 隐马尔可夫模型
    《统计学习方法》笔记九 EM算法及其推广
    《统计学习方法》笔记八 提升方法
    《统计学习方法》笔记七(3) 支持向量机——非线性支持向量机
    拜拜~~~
    拓扑排序+DP CF721C Journey
    数学 CF1068B LCM
    扩展欧拉定理【洛谷P4139】 上帝与集合的正确用法
    浅谈扩展欧拉定理
    数论 CF230B T-primes
  • 原文地址:https://www.cnblogs.com/jiawen010/p/10156314.html
Copyright © 2011-2022 走看看