Python----装饰器的使用方法(python装饰器--原来如此简单)

网友投稿 210 2022-09-04


Python----装饰器的使用方法(python装饰器--原来如此简单)

根据装饰器和被装饰对象的不同类型,装饰器的使用场景主要有以下情况

装饰器是函数,

被装饰对象也是函数

装饰器无参数,被装饰对象无参数装饰器无参数,被装饰对象有参数装饰器有参数,被装饰对象无参数装饰器有参数,被装饰器对象有参数

被装饰对象是类

装饰器无参数,被装饰类无参数装饰器无参数,被装饰类有参数装饰器有参数,被装饰类无参数装饰器有参数,被装饰类有参数

被装饰对象是类中的方法

装饰器无参数,被装饰类中的方法无参数装饰器无参数,被装饰类中的方法有参数装饰器有参数,被装饰类中的方法无参数装饰器有参数,被装饰类中的方法有参数

装饰器是类

被装饰对象也是函数

装饰器无参数,被装饰对象无参数装饰器无参数,被装饰对象有参数装饰器有参数,被装饰对象无参数装饰器有参数,被装饰器对象有参数

被装饰器对象是类

装饰器无参数,被装饰类无参数装饰器无参数,被装饰类有参数装饰器有参数,被装饰类无参数装饰器有参数,被装饰类有参数

被装饰对象是类中的方法

装饰器无参数,被装饰类中的方法无参数装饰器无参数,被装饰类中的方法有参数装饰器有参数,被装饰类中的方法无参数装饰器有参数,被装饰类中的方法有参数

实例演示不同场景下的装饰器

装饰器是函数,

被装饰对象也是函数装饰器是函数,被装饰对象是函数时,此时装饰器的作用是给被装饰对象的函数做功能增强功能,思想是面向切面编程思想,即在被装饰对象的函数的之前和之后做一些额外处理,而不破坏被装饰函数原有的功能实现,主要有以下几种情况:

装饰器无参数,被装饰对象无参数

def decorator(func): def _decorator(): print("before func......") func() print("after func......") return _decorator@decoratordef func(): print("in func()......")func()

执行结果为:

before func......in func()......after func......

装饰器无参数,被装饰对象有参数

def decorator(func): def _decorator(*args,**kwargs): print("before {name}......".format(name=func.__name__)) func(*args,**kwargs) print("after {name}......".format(name=func.__name__)) return _decorator@decoratordef func1(): print("in func1()......")@decoratordef func2(a,b): print("in func2()......") print("a={a},b={b}".format(a=a,b=b))@decoratordef func3(a,b,c=10): print("in func3()......") print("a={a},b={b}".format(a=a, b=b)) print("c={c}".format(c=c))func1()func2(1,2)func3(1,2,c=100)

运行结果为:

before func1......in func1()......after func1......before func2......in func2()......a=1,b=2after func2......before func3......in func3()......a=1,b=2c=100after func3......

装饰器有参数,被装饰对象无参数

def wrapper(name): def decorator(func): def _decorator(): print("before {name} ,decorator param is {param}".format(name=func.__name__,param=name)) print("before {name}......".format(name=func.__name__)) func() print("after {name}......".format(name=func.__name__)) return _decorator return decorator@wrapper(name="hello world")def func1(): print("in func1()......")func1()

运行结果为:

before func1 ,decorator param is hello worldbefore func1......in func1()......after func1......

装饰器有参数,被装饰器对象有参数

def wrapper(name): def decorator(func): def _decorator(*args,**kwargs): print("before {name} ,decorator param is {param}".format(name=func.__name__,param=name)) print("before {name}......".format(name=func.__name__)) func(*args,**kwargs) print("after {name}......".format(name=func.__name__)) return _decorator return decorator@wrapper(name="hello world")def func1(): print("in func1()......")@wrapper(name="hello world")def func2(a, b): print("in func2()......") print("a={a},b={b}".format(a=a, b=b))@wrapper(name="hello world")def func3(a, b, c=10): print("in func3()......") print("a={a},b={b}".format(a=a, b=b)) print("c={c}".format(c=c))func1()func2(1,2)func3(1,2,c=100)

运行结果如下:

before func1 ,decorator param is hello worldbefore func1......in func1()......after func1......before func2 ,decorator param is hello worldbefore func2......in func2()......a=1,b=2after func2......before func3 ,decorator param is hello worldbefore func3......in func3()......a=1,b=2c=100after func3......

被装饰器对象是类被装饰器对象为类时,装饰器的作用是类在初始化的时候做功能增强,即可以在类初始化之前或者之后做一些功能增强,所以下面所说的被装饰对象有无参数是针对类的初始化函数__init__有无参数而言的,主要有以下几种情况:

装饰器无参数,被装饰类无参数

def decorator(cls): def _decorator(): print("before class {name} init......".format(name=cls.__name__)) obj=cls() print("after class {name} init......".format(name=cls.__name__)) return obj return _decorator@decoratorclass Test(object): def __init__(self): print("in class {name} init function......".format(name=Test.__name__)) def func(self): print("in class {name} func()".format(name=Test.__name__))test=Test()test.func()

运行结果如下:

before class Test init......in class _decorator init function......after class Test init......in class _decorator func()

装饰器无参数,被装饰类有参数

def decorator(cls): def _decorator(*args,**kwargs): print("before class {name} init......".format(name=cls.__name__)) obj=cls(*args,**kwargs) print("after class {name} init......".format(name=cls.__name__)) return obj return _decorator@decoratorclass Test(object): def __init__(self,a,b=10): print("in class {name} init function......".format(name=Test.__name__)) print("a={a},b={b}".format(a=a,b=b)) def func(self): print("in class {name} func()".format(name=Test.__name__))test=Test(2,20)test.func()

执行结果如下:

before class Test init......in class _decorator init function......a=2,b=20after class Test init......in class _decorator func()

装饰器有参数,被装饰类无参数

def wrapper(name="hello world"): def decorator(cls): def _decorator(): print("before class {name} init......".format(name=cls.__name__)) obj=cls() print("after class {name} init......".format(name=cls.__name__)) return obj return _decorator return decorator@wrapper("hello world")class Test(object): def __init__(self): print("in class {name} init function......".format(name=Test.__name__)) def func(self): print("in class {name} func()".format(name=Test.__name__))test=Test()test.func()

执行结果如下:

before class Test init......in class _decorator init function......after class Test init......in class _decorator func()

装饰器有参数,被装饰类有参数

def wrapper(name="hello world"): def decorator(cls): def _decorator(*args,**kwargs): print("before class {name} init......".format(name=cls.__name__)) obj=cls(*args,**kwargs) print("after class {name} init......".format(name=cls.__name__)) return obj return _decorator return decorator@wrapper("hello world")class Test(object): def __init__(self,a,b=10): print("in class {name} init function......".format(name=Test.__name__)) print("a= {a} b={b}".format(a=a,b=b)) def func(self): print("in class {name} func()".format(name=Test.__name__))test=Test(2,b=20)test.func()``运行结果如下:```pythonbefore class Test init......in class _decorator init function......a= 2 b=20after class Test init......in class _decorator func()

被装饰对象是类中的方法

装饰器无参数,被装饰类中的方法无参数

def decorator(func): def _decorator(self): print("before class {name} init......".format(name=func.__name__)) obj=func(self) print("after class {name} init......".format(name=func.__name__)) return obj return _decoratorclass Test(object): def __init__(self,a=1,b=10): self.a=a self.b=b @decorator def func(self): print("in class {name} func()".format(name=Test.__name__)) print("a={a} b={b}".format(a=self.a,b=self.b))test=Test()test.func()

执行结果如下:

before class func init......in class Test func()a=1 b=10after class func init......

装饰器无参数,被装饰类中的方法有参数

def decorator(func): def _decorator(self,*args,**kwargs): print("before class {name} init......".format(name=func.__name__)) obj=func(self,*args,**kwargs) print("after class {name} init......".format(name=func.__name__)) return obj return _decoratorclass Test(object): def __init__(self,a=1,b=10): self.a=a self.b=b @decorator def func(self,c,d=20): print("in class {name} func()".format(name=Test.__name__)) print("a={a} b={b}".format(a=self.a,b=self.b)) print("c={c} d={d}".format(c=c,d=d))test=Test()test.func(2,d=20)

执行结果如下:

before class func init......in class Test func()a=1 b=10c=2 d=20after class func init......

装饰器有参数,被装饰类中的方法无参数

def wrapper(name="hello world"): def decorator(func): def _decorator(self): print("before class {name} init......".format(name=func.__name__)) print("name = {name}".format(name=name)) obj=func(self) print("after class {name} init......".format(name=func.__name__)) return obj return _decorator return decoratorclass Test(object): def __init__(self,a=1,b=10): self.a=a self.b=b @wrapper(name="Test.func") def func(self): print("in class {name} func()".format(name=Test.__name__)) print("a={a} b={b}".format(a=self.a,b=self.b))test=Test()test.func()

执行结果如下:

before class func init......name = Test.funcin class Test func()a=1 b=10after class func init......

装饰器有参数,被装饰类中的方法有参数

def wrapper(name="hello world"): def decorator(func): def _decorator(self,*args,**kwargs): print("before class {name} init......".format(name=func.__name__)) print("name = {name}".format(name=name)) obj=func(self,*args,**kwargs) print("after class {name} init......".format(name=func.__name__)) return obj return _decorator return decoratorclass Test(object): def __init__(self,a=1,b=10): self.a=a self.b=b @wrapper(name="Test.func") def func(self,c,d=20): print("in class {name} func()".format(name=Test.__name__)) print("a={a} b={b}".format(a=self.a,b=self.b)) print("c={c} d={d}".format(c=c,d=d))test=Test()test.func(2,d=20)``执行结果如下:```pythonbefore class func init......name = Test.funcin class Test func()a=1 b=10c=2 d=20after class func init......

装饰器是类

被装饰对象是函数装饰器是类,被装饰对象是函数的时候,是通过装饰器类初始化的时候将被装饰对象函数传递给装饰器类,然后通过自动调用装饰器类中的__call__函数实现对被装饰对象的前后处理,即在这种情况下,只需要在__call__函数中编写在调用被装饰对象前后进行操作的代码即可

装饰器无参数,被装饰对象无参数

class decorator(object): def __init__(self,func): self.func=func def __call__(self): print("before func {func}()...".format(func=self.func.__name__)) result=self.func() print("after func {func}()...".format(func=self.func.__name__)) return result@decoratordef func(): print("in func func()...")func()

执行结果如下:

before func func()...in func func()...after func func()...

装饰器无参数,被装饰对象有参数

class decorator(object): def __init__(self,func): self.func=func def __call__(self,*args,**kwargs): print("before func {func}()...".format(func=self.func.__name__)) result=self.func(*args,**kwargs) print("after func {func}()...".format(func=self.func.__name__)) return result@decoratordef func(a,b=10): print("in func func()...") print("a= {a} b={b}".format(a=a,b=b))func(2,b=20)``执行结果如下:```pythonbefore func func()...in func func()...a= 2 b=20after func func()...

装饰器有参数,被装饰对象无参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,func): def wrapper(): print("before func {func}()...".format(func=func.__name__)) print("name= {name}".format(name=self.name)) result=func() print("after func {func}()...".format(func=func.__name__)) return result return wrapper@decorator()def func1(): print("in func func2()...")@decorator("func2_decorator")def func2(): print("in func func2()...")@decorator(name="func3_decorator")def func3(): print("in func func3()...")func1()func2()func3()

执行结果如下:

before func func1()...name= hello worldin func func2()...after func func1()...before func func2()...name= func2_decoratorin func func2()...after func func2()...before func func3()...name= func3_decoratorin func func3()...after func func3()...

装饰器有参数,被装饰器对象有参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,func): def wrapper(*args,**kwargs): print("before func {func}()...".format(func=func.__name__)) print("name= {name}".format(name=self.name)) result=func(*args,**kwargs) print("after func {func}()...".format(func=func.__name__)) return result return wrapper@decorator()def func1(): print("in func func2()...")@decorator("func2_decorator")def func2(a): print("in func func2()...") print("a={a}".format(a=a))@decorator(name="func3_decorator")def func3(a,b=10): print("in func func3()...") print("a={a}, b= {b}".format(a=a,b=b))func1()func2(10)func3(2,b=20)

执行结果如下:

before func func1()...name= hello worldin func func2()...after func func1()...before func func2()...name= func2_decoratorin func func2()...a=10after func func2()...before func func3()...name= func3_decoratorin func func3()...a=2, b= 20after func func3()...

被装饰器对象是类

装饰器无参数,被装饰类无参数

class decorator(object): def __init__(self,cls): self.cls=cls def __call__(self): print("before init clsss {cls}...".format(cls=self.cls.__name__)) obj=self.cls() print("after init class {cls}...".format(cls=self.cls.__name__)) return obj@decoratorclass Test(): def __init__(self): print("in Test class __init__ func...") def test(self): print("in Test class test func...")t=Test()t.test()

执行结果如下:

before init clsss Test...in Test class __init__ func...after init class Test...in Test class test func...

装饰器无参数,被装饰类有参数

class decorator(object): def __init__(self,cls): self.cls=cls def __call__(self,*args,**kwargs): print("before init clsss {cls}...".format(cls=self.cls.__name__)) obj=self.cls(*args,**kwargs) print("after init class {cls}...".format(cls=self.cls.__name__)) return obj@decoratorclass Test(): def __init__(self,a,b=10): print("in Test class __init__ func...") print("a={a} b={b}".format(a=a,b=b)) def test(self): print("in Test class test func...")t=Test(2,b=20)t.test()

执行结果如下:

before init clsss Test...in Test class __init__ func...a=2 b=20after init class Test...in Test class test func...

装饰器有参数,被装饰类无参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,cls): def wrapper(): print("before init class {cls}....".format(cls=cls.__name__)) obj=cls() print("after init class {cls}...".format(cls=cls.__name__)) return obj return wrapper@decorator(name="Test Class")class Test(): def __init__(self): print("in Test class __init__ func...") def test(self): print("in Test class test func...")t=Test()t.test()

执行结果如下:

before init class Test....in Test class __init__ func...after init class Test...in Test class test func...

装饰器有参数,被装饰类有参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,cls): def wrapper(*args,**kwargs): print("before init class {cls}....".format(cls=cls.__name__)) obj=cls(*args,**kwargs) print("after init class {cls}...".format(cls=cls.__name__)) return obj return wrapper@decorator(name="Test Class")class Test(): def __init__(self,a,b=10): print("in Test class __init__ func...") print("a={a}, b={b}".format(a=a,b=b)) def test(self): print("in Test class test func...")t=Test(2,b=20)t.test()

执行结果如下:

before init class Test....in Test class __init__ func...a=2, b=20after init class Test...in Test class test func...

被装饰对象是类中的方法

装饰器无参数,被装饰类中的方法无参数

class decorator(object): def __init__(self,func): self.func=func def __call__(self): print("before func {func}...".format(func=self.func.__name__)) results=self.func(self) print("after func {func}...".format(func=self.func.__name__)) return resultsclass Test(): def __init__(self): print("in Test class __init__ func...") @decorator def test(self): print("in Test class test func...")t=Test()t.test()

执行结果如下:

in Test class __init__ func...before func test...in Test class test func...after func test...

装饰器无参数,被装饰类中的方法有参数 ``python class decorator(object): def init(self,func): self.func=func

def __call__(self,*args,**kwargs): print("before func {func}...".format(func=self.func.__name__)) results=self.func(self,*args,**kwargs) print("after func {func}...".format(func=self.func.__name__)) return results

class Test(): def init(self): print(“in Test class init func…”)

@decoratordef test(self,a,b=10): print("in Test class test func...") print("a={a} b={b}".format(a=a,b=b))

t=Test() t.test(2,b=20)

执行结果如下:```pythonin Test class __init__ func...before func test...in Test class test func...a=2 b=20after func test...

装饰器有参数,被装饰类中的方法无参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,func): def wrapper(self): print("before func {func}...".format(func=func.__name__)) results=func(self) print("after func {func}...".format(func=func.__name__)) return results return wrapperclass Test(): def __init__(self): print("in Test class __init__ func...") @decorator(name="Test_test") def test(self): print("in Test class test func...")t=Test()t.test()

执行结果如下:

in Test class __init__ func...before func test...in Test class test func...after func test...

装饰器有参数,被装饰类中的方法有参数

class decorator(object): def __init__(self,name="hello world"): self.name=name def __call__(self,func): def wrapper(self,*args,**kwargs): print("before func {func}...".format(func=func.__name__)) results=func(self,*args,**kwargs) print("after func {func}...".format(func=func.__name__)) return results return wrapperclass Test(): def __init__(self): print("in Test class __init__ func...") @decorator(name="Test_test") def test(self,a,b=10): print("in Test class test func...") print("a={a}, b={b}".format(a=a,b=b))t=Test()t.test(2,b=20)

执行结果如下:

in Test class __init__ func...before func test...in Test class test func...a=2, b=20after func test...


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java数组实现循环队列示例介绍
下一篇:【图像加密】基于Logistic混沌结合Arnold置乱实现图像加密含Matlab源码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~