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

由浅入深,走进Python装饰器-----第五篇:进阶--类装饰类

时间:2019-05-02 18:31:09      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:func   python   函数   改变   参数   代码   使用   int   python装饰器   

**类装饰器** @类 类

4.1 用类装饰器来扩展原类( 增加属性和方法 )

# 用类装饰器来扩展原函数,  通过对象函数化触发__call__方法,进行返回
class KuoZhan():
    def __call__(self,cls):
        return self.newfunc(cls)
    def good(self):
        print("新增的方法!")
    def newfunc(self,cls):
        def in_newfunc(): 
            cls.addpty = "新增的属性"
            cls.good = KuoZhan.good
            # 此处返回的是一个实例化对象
            return cls()
        return in_newfunc

@KuoZhan()        #1.  KuoZhan() ==> obj    2. @KuoZhan()==> @obj  ==>  obj( )   3.  @KuoZhan() == obj( MyClass), ,触发__call__ ,得到 in_newfunc
class MyClass():
    def func(self):
        print("我是原类的方法")
# MyClass实际就是in_newfunc, MyClass() == in_newfunc() , 所以 obj = cls()
# 对象调用方式
obj = MyClass() 
obj.func()
obj.good()
print(obj.addpty)

>>>我是原类的方法
>>>新增的方法啦!
>>>新增的属性

4.2 用类装饰器来扩展原类( 增加属性和方法 )

# 用类装饰器来扩展原函数, 直接通过类方法修饰后,进行返回
class KuoZhan():
    def good():
        print("新增的方法啦!")
    def newfunc(cls):
        def in_newfunc(): 
            cls.addpty = "新增的属性"
            cls.good = KuoZhan.good
            # 此处返回的是一个类
            return cls
        return in_newfunc
# 类.方法 的方式      
@KuoZhan.newfunc        #1. MyClass = KuoZhan.newfunc(MyClass) ==> MyClass = in_newfunc 
class MyClass():
    def func():
        print("我是原类的方法")

obj = MyClass()
obj.func()
obj.good()
print(obj.addpty)
>>>我是原类的方法
>>>新增的方法啦!
>>>新增的属性

4.3 用类装饰器来扩展原类( 改变属性和方法 )

# 用类装饰器来扩展原函数,  通过对象函数化触发__call__方法,进行返回
class KuoZhan():
    def __call__(self,cls):
        return self.newfunc(cls)
    def func(self):
        print("新增的方法!")
    def newfunc(self,cls):
        def in_newfunc(): 
            cls.addpty = "新增的属性"
            cls.func = KuoZhan.func
            # 此处将原方法变成属性
            cls.func2 = cls.func2(self)
            # 此处返回的是一个实例化对象,使用的是绑定对象方法,所以上句代码使用绑定对象方法
            return cls()
        return in_newfunc

@KuoZhan()        #1.  KuoZhan() ==> obj    2. @KuoZhan()==> @obj  ==>  obj( )   3.  @KuoZhan() == obj( MyClass), ,触发__call__ ,得到 in_newfunc
class MyClass():
    addpty = "原有的属性"
    def func(self):
        print("我是原类的方法")
    def func2(self):
        return "我是原类的方法2"
# MyClass实际就是in_newfunc, MyClass() == in_newfunc() , 所以 obj = cls()
obj = MyClass() 
obj.func()
print(obj.addpty)
print(obj.func2)

>>>新增的方法!
>>>新增的属性
>>>我是原类的方法2

4.4 用类装饰器来扩展原类( 改变属性和方法 )

# 用类装饰器来扩展原函数,  通过直接调用类方法,进行返回
class KuoZhan():
    def func():
        print("新增的方法!")
    def newfunc(cls):
        def in_newfunc(): 
            cls.addpty = "新增的属性"
            cls.func = KuoZhan.func
            # 注意直接使用类方法,不用额外参数
            cls.func2 = cls.func2()
            # 此处返回的是一个类
            return cls
        return in_newfunc
# 类.方法的方式       
@KuoZhan.newfunc        #1. MyClass = KuoZhan.newfunc(MyClass) ==> MyClass = in_newfunc
class MyClass():
    addpty = "原有的属性"
    def func():
        print("我是原类的方法")
    def func2():
        return "我是原类的方法2"
# MyClass实际就是in_newfunc, MyClass() == in_newfunc() , 所以 obj = cls()
obj = MyClass()
obj.func()
print(obj.addpty)
print(obj.func2)

>>>新增的方法!
>>>新增的属性
>>>我是原类的方法2

由浅入深,走进Python装饰器-----第五篇:进阶--类装饰类

标签:func   python   函数   改变   参数   代码   使用   int   python装饰器   

原文地址:https://blog.51cto.com/dldxzjr/2388362

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