Python 装饰器原理、定义与用法详解

 更新时间:2019年12月07日 10:18:31   作者:code_designer  
这篇文章主要介绍了Python 装饰器原理、定义与用法,结合实例形式分析了Python装饰器的概念、定义、实现方法、应用场景及相关操作注意事项,需要的朋友可以参考下

本文实例讲述了Python 装饰器原理、定义与用法。分享给大家供大家参考,具体如下:

Python 装饰器

一、何为装饰器

1、在函数中定义函数

在函数中定义另外的函数,就是说可以创建嵌套的函数,例子如下

def sayHi(name="hjj2"):
 print 'inside sayHi() func'
 def greet():
  return 'inside greet() func'
 print(greet())
sayHi()
#output
#  inside sayHi() func
#  inside greet() func

2、将函数作为参数传给另外一个函数,装饰器原型

def sayHi():
 return 'hi hjj2'
def doSthBeforeSayHi(func):
 print 'before sayHi func'
 print(func())
doSthBeforeSayHi(sayHi)
#output
#  before sayHi func
#  hi hjj2

3、实现一个装饰器

在第二步中,我们已经基本探究到装饰器的原理了,python装饰器做的事就是通过封装一个函数并且用这样或那样的方式来修改它的行为。不带@的初步示例如下:

def new_decorator(func):
  def wrapDecorator():
   print 'before func'
   func()
   print 'after func'
  return wrapDecorator
def func_require_decorator():
  print 'a func need decorator'
func_require_decorator()
#ouput: a func need decorator
func_require_decorator = new_decorator(func_require_decorator)
func_require_decorator()
#ouput:
#  before func
#  a func need decorator
#  after func

使用@来运行装饰器

@new_decorator
func_require_decorator()
#ouput:
#  before func
#  a func need decorator
#  after func

这里我们可以看到,这两个例子的运行结果是一样的。所以我们能想象得到@new_decorator的作用就是

func_require_decorator = new_decorator(func_require_decorator)

我们继续优化这个装饰器,现在我们有一个问题就是,如果我们想要通过print(func_require_decorator.__name__)就会报错# Output: wrapTheFunction。这样就需要借助python提供的functools.wraps来解决了

@wraps接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

from functools import wraps
def new_decorator(func):
  @wraps(func)
  def wrapDecorator():
   print 'before func'
   func()
   print 'after func'
  return wrapDecorator
def func_require_decorator():
  print 'a func need decorator'
@new_decorator
func_require_decorator()
print(func_require_decorator.__name__)
#ouput: func_require_decorator

二、使用场景

1、授权,大体例子

from functools import wraps
def requires_auth(f):
  @wraps(f)
  def decorated(*args, **kwargs):
    auth = request.authorization
    if not auth or not check_auth(auth.username, auth.password):
      authenticate()
    return f(*args, **kwargs)
  return decorated

2、日志:

from functools import wraps
def logit(logfile='out.log'):
  def logging_decorator(func):
    @wraps(func)
    def wrapped_function(*args,**kwargs):
      log_string = func.__name__+"was called"
      print(log_string)
      with open(logfile,'a') as opened_file:
        opened_file.write(log_string+'\n')
      return func(*args,**kwargs)
    return wrapped_function
  return logging_decorator
@logit()
def func1():
  pass
func1()

3、其他如flask中的@app.route()

三、装饰器类

1、将上面的日志装饰器变为类的初步模型如下

from functools import wraps
class logit(object):
  def __init__(self, logfile='out.log'):
    self.logfile = logfile
  def __call__(self, func):
    @wraps(func)
    def wrapped_function(*args, **kwargs):
      log_string = func.__name__ + "was called"
      print(log_string)
      # 打开logfile并写入
      with open(self.logfile, 'a') as open_file:
        # 将日志写到指定文件
        open_file.write(log_string + '\n')
      # 发送一个通知
      self.notify()
      return func(*args, **kwargs)
    return wrapped_function
  def notify(self):
    pass
@logit()
def myfunc1():
  pass
class email_logit(logit):
  '''
  实现在函数调用时发送email
  '''
  def __init__(self, email='admin@xxx.com', *args, **kwargs):
    self.email = email
    super(email_logit, self).__init__(*args, **kwargs)
  def notify(self):
    '''
    发送邮件通知
    '''
    pass

通过这种方式,我们可以定义我们在自己的需求,减少代码的冗余,提高复用率。

至此,关于装饰器的探索就结束啦。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • Appium+Python自动化环境搭建实例教程

    Appium+Python自动化环境搭建实例教程

    这篇文章主要介绍了Appium+Python自动化环境搭建实例教程,本文通过实例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • 浅谈python之高阶函数和匿名函数

    浅谈python之高阶函数和匿名函数

    这篇文章主要介绍了python之高阶函数和匿名函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Python操作列表之List.insert()方法的使用

    Python操作列表之List.insert()方法的使用

    这篇文章主要介绍了Python操作列表之List.insert()方法的使用,是Python入门中的基础知识,需要的朋友可以参考下
    2015-05-05
  • 用Python解数独的方法示例

    用Python解数独的方法示例

    这篇文章主要介绍了用Python解数独的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • python开发之tkinter实现图形随鼠标移动的方法

    python开发之tkinter实现图形随鼠标移动的方法

    这篇文章主要介绍了python开发之tkinter实现图形随鼠标移动的方法,涉及Python基于tkinter绘图的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • 使用Python脚本实现批量网站存活检测遇到问题及解决方法

    使用Python脚本实现批量网站存活检测遇到问题及解决方法

    本文是小编自己编写的一个使用python实现批量网站存活检测。在项目测试中非常有用。本文给大家分享了遇到的问题及解决方案,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-10-10
  • 在spyder IPython console中,运行代码加入参数的实例

    在spyder IPython console中,运行代码加入参数的实例

    这篇文章主要介绍了在spyder IPython console中,运行代码加入参数的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • pytorch plt.savefig()的用法及保存路径

    pytorch plt.savefig()的用法及保存路径

    这篇文章主要给大家介绍了关于pytorch plt.savefig()的用法及保存路径的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-01-01
  • python2.7的flask框架之引用js&css等静态文件的实现方法

    python2.7的flask框架之引用js&css等静态文件的实现方法

    今天小编就为大家分享一篇python2.7的flask框架之引用js&css等静态文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • PyTorch 如何设置随机数种子使结果可复现

    PyTorch 如何设置随机数种子使结果可复现

    这篇文章主要介绍了PyTorch 设置随机数种子使结果可复现操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05

最新评论