Python 装饰器学习以及实际使用场景实践

开发 后端
前几天在看Flask框架,,不是非常的理解,回来补装饰器的功课。阅读很多的关于装饰器的文章,自己整理一下,适合自己的思路的方法和例子,与大家分享。

前言

前几天在看Flask框架,,不是非常的理解,回来补装饰器的功课。阅读很多的关于装饰器的文章,自己整理一下,适合自己的思路的方法和例子,与大家分享。

Python 装饰器学习以及实际使用场景实践

 

  1. app = Flask(__name__) 
  2. @app.route("/"
  3. def hello(): 
  4. return "Hello World!" 

1、装饰器是什么

装饰器是Python语言中的高级语法。主要的功能是对一个函数、方法、或者类进行加工,作用是为已经存在的对象添加额外的功能,提升代码的可读性。
装饰器是设计模式的一种,被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等

2、装饰器的语法

装饰器的语法如下:

当前Python的装饰器语法如下:

  1. @dec2 
  2. @dec1 
  3. def func(arg1, arg2, ...): 
  4.     .... 
  5.     return funx 
  6.  
  7. 上面的代码相当于: 
  8.  
  9. def func(arg1, arg2, ...): 
  10.     pass 
  11. func = dec2(dec1(func)) 

装饰器可以用def的形式来定义。装饰器接收一个可调用对象作为输入参数,并返回一个新的可调用对象。
装饰器新建了一个可调用对象,也就是return 返回的函数funx,在新增的函数中,可以添加我们需要的功能,并通过调用原有函数来实现原有函数的功能。

3、装饰器的使用

3.1不带参数的装饰器

定义装饰器非常的简单:

  1. def deco(func): 
  2.     """无参数调用decorator声明时必须有一个参数,这个参数将接收要装饰的方法""" 
  3.     print "before myfunc() called." 
  4.     func() 
  5.     print "after myfunc() called." 
  6.     return func 
  7.  
  8. @deco 
  9. def myfunc(): 
  10.     print " myfunc() called." 
  11.  
  12.  
  13. myfunc() 
  14. myfunc() 

定义好装饰器后,,,即可使用。上面这个装饰器在使用的时候有一个问题,即只在***次被调用,并且原来的函数多执行一次。执行输出如下:

 

  1. before myfunc() called.   
  2. myfunc() called. 
  3. after myfunc() called. 
  4.  myfunc() called.   --函数多执行一次的输出 
  5.  myfunc() called.   --第二次调用,装饰器不生效 

要保证新函数每次被调用,使用下面的方法来定义装饰器

 

  1. def deco(func): 
  2.     """无参数调用decorator声明时必须有一个参数,这个参数将接收要装饰的方法""" 
  3.     def _deco(): 
  4.         print "before myfunc() called." 
  5.         func() 
  6.         print "after myfunc() called." 
  7.         #return func 不需要返回func 
  8.     retrun _deco 
  9. @deco 
  10. def myfunc(): 
  11.     print " myfunc() called." 
  12.     return 'OK' 
  13. myfunc() 
  14. myfunc() 

函数输出如下:

 

  1. before myfunc() called. 
  2.  myfunc() called. 
  3.   after myfunc() called. 
  4. before myfunc() called. 
  5.  myfunc() called. 
  6.   after myfunc() called. 

这样可以看到,装饰器每次都得到了调用。

3.2带参数的函数进行装饰器

 

  1. def deco(func): 
  2.     def _deco(a, b): 
  3.         print("before myfunc() called."
  4.         ret = func(a, b) 
  5.         print("  after myfunc() called. result: %s" % ret) 
  6.     return ret 
  7. return _deco 
  8.  
  9. @deco 
  10. def myfunc(a, b): 
  11.     print(" myfunc(%s,%s) called." % (a, b)) 
  12.     return a + b 
  13.   
  14. myfunc(12
  15. myfunc(34

输出:

 

  1. before myfunc() called. 
  2. myfunc() called. 
  3. After myfunc() called. result: 3 
  4. before myfunc() called. myfunc() called. After myfunc() called. result: 7 

内嵌函数的形参和返回值与原函数相同,装饰函数返回内嵌包装函数。

3.3装饰器带参数

 

  1. def decoWithArgs(arg): 
  2. """由于有参数的decorator函数在调用时只会使用应用时的参数而不接收被装饰的函数做为参数, 
  3.    所以必须返回一个decorator函数, 由它对被装饰的函数进行封装处理""
  4. def newDeco(func):    #定义一个新的decorator函数 
  5.     def replaceFunc():    #在decorator函数里面再定义一个内嵌函数,由它封装具体的操作 
  6.         print "Enter decorator %s" %arg    #进行额外操作 
  7.         return func()    #对被装饰函数进行调用 
  8.     return replaceFunc 
  9. return newDeco    #返回一个新的decorator函数 
  10.  
  11. @decoWithArgs("demo"
  12. def MyFunc():    #应用@decoWithArgs修饰的方法 
  13.     print "Enter MyFunc" 
  14.  
  15. MyFunc()    #调用被装饰的函数 

输出:
nter decorator demo
Enter MyFunc

这个情形适用于原来的函数没有参数,新增加打印的情况。常见适用的地方是增加函数的打印日志。

3.4对参数数量不确定的函数进行装饰

下面的例子是一个邮件异步发送的例子,函数的参数数据部确定,装饰器实现了对于邮件发送函数的异步发送。

 

  1. from threading import Thread 
  2.  
  3. def async(f): 
  4.     def wrapper(*args, **kwargs): 
  5.         thr = Thread(target = f, args = args, kwargs = kwargs) 
  6.         thr.start() 
  7.     return wrapper 
  8.  
  9. @async 
  10. def send_async_email(msg): 
  11.     mail.send(msg) 
  12.  
  13. def send_email(subject, sender, recipients, text_body, html_body): 
  14.     msg = Message(subject, sender = sender, recipients = recipients) 
  15.     msg.body = text_body 
  16.     msg.html = html_body 
  17.     send_async_email(msg) 

并且这个装饰器可以适用一切需要异步处理的功能,做到非常好的代码复用。

3.5让装饰器带类参数

 

  1. class locker: 
  2.     def __init__(self): 
  3.         print("locker.__init__() should be not called."
  4.           
  5.     @staticmethod 
  6.     def acquire(): 
  7.         print("locker.acquire() called.(这是静态方法)"
  8.           
  9.     @staticmethod 
  10.     def release(): 
  11.         print("  locker.release() called.(不需要对象实例)"
  12.   
  13. def deco(cls): 
  14.     '''cls 必须实现acquire和release静态方法''' 
  15.     def _deco(func): 
  16.         def __deco(): 
  17.             print("before %s called [%s]." % (func.__name__, cls)) 
  18.             cls.acquire() 
  19.             try
  20.                 return func() 
  21.             finally
  22.                 cls.release() 
  23.         return __deco 
  24.     return _deco 
  25.   
  26. @deco(locker) 
  27. def myfunc(): 
  28.     print(" myfunc() called."
  29.   
  30. myfunc() 
  31. myfunc() 

输出为:

 

  1. before myfunc called [__main__.locker]. 
  2. locker.acquire() called.(this is staticmethon) 
  3. myfunc() called. 
  4.   locker.release() called.(do't need object ) 
  5.  
  6. before myfunc called [__main__.locker]. 
  7. locker.acquire() called.(this is staticmethon) 
  8. myfunc() called. 
  9.   locker.release() called.(do't need object ) 

装饰器总结

当我们对某个方法应用了装饰方法后, 其实就改变了被装饰函数名称所引用的函数代码块入口点,使其重新指向了由装饰方法所返回的函数入口点。由此我们可以用decorator改变某个原有函数的功能,添加各种操作,或者完全改变原有实现。

参考文章

感谢以下几位大神:

http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html

http://www.cnblogs.com/Jifangliang/archive/2008/07/22/1248313.html

http://www.cnblogs.com/vamei/archive/2013/02/16/2820212.html

 

责任编辑:王雪燕 来源: 博客园
相关推荐

2015-06-16 13:52:25

Mesos集群管理Hadoop

2020-04-07 14:20:10

RabbitMMySQL数据库

2023-11-15 13:35:00

迭代器生成器Python

2009-12-25 17:33:19

WPF TextBlo

2010-04-29 10:48:10

Oracle序列

2022-10-28 07:15:26

策略模式使用场景UML

2010-02-01 17:50:32

Python装饰器

2023-11-17 16:06:14

2010-03-12 09:28:37

ATM交换机

2021-08-29 22:05:04

对象自动回收

2018-05-16 15:26:43

数据库MySQL主从复制

2012-10-22 13:18:05

KVM

2023-06-06 08:18:24

Kafka架构应用场景

2020-10-29 07:16:26

布隆过滤器场景

2010-03-18 17:57:37

Java XMLSoc

2023-05-16 07:47:18

RabbitMQ消息队列系统

2018-08-15 09:48:27

数据库Redis应用场景

2021-09-10 06:50:03

TypeScript装饰器应用

2010-07-06 09:24:20

BizTalk Ser

2018-12-03 12:20:52

Systemd定时器Linux
点赞
收藏

51CTO技术栈公众号