深入了解Python装饰器的高级用法


Posted in Python onAugust 13, 2020

原文地址
https://www.codementor.io/python/tutorial/advanced-use-python-decorators-class-function

介绍

我写这篇文章的主要目的是介绍装饰器的高级用法。如果你对装饰器知之甚少,或者对本文讲到的知识点易混淆。我建议你复习下装饰器基础教程。
本教程的目标是介绍装饰器的一些有趣的用法。特别是怎样在类中使用装饰器,怎样给装饰器传递额外的参数。

装饰器 vs 装饰器模式

Decorator模式是一个面向对象的设计模式,它允许动态地往现有的对象添加行为。当你装饰了一个对象,在某种程度上,你是在独立于同一个类的其他实例的基础上扩展其功能。
Python装饰器不是装饰器模式的实现,它在函数、方法定义的时候添加功能,而不是在运行的时候添加。Decorator设计模式本身可以在Python中实现,因为Python是动态编程语言,所以没有必要这样做。

一个基础的装饰器

这是装饰器的最简单例子,在继续往下面阅读之前请确保理解此段代码。如果你需要更多关于此代码的解释,请复习下基础装饰器教程。

def time_this(original_function): 
  def new_function(*args, **kwargs):
    import datetime 
    before = datetime.datetime.now() 
    x = original_function(*args, **kwargs) 
    after = datetime.datetime.now() 
    print("Elapsed Time = {}".format(after-before)) 
    return x 
  return new_function
@time_this
def func_a(stuff): 
  import time 
  time.sleep(stuff) 
  func_a(3)
# out:
Elapsed Time = 0:00:03.012472

带参数的装饰器

有时候带参数的装饰器会非常有用,这种技术经常用在函数注册中。在web框架Pyramid中经常有用到,例如:

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request): 
  return {'project': 'hello decorators'}

比方说,我们有一个用户可以登录并且可以和用户交互的GUI应用程序。用户和GUI界面的交互触发事件,导致Python函数执行。假设有许多使用该图形界面的用户,他们各自的权限级别差异很大,不同的功能执行需要不同的权限。比如,考虑以下功能:

# 假设这些函数是存在的
def current_user_id(): 
  """ this function returns the current logged in user id, if the use is not authenticated the return None """
def get_permissions(iUserId): 
  """ returns a list of permission strings for the given user. For example ['logged_in','administrator','premium_member'] """
# 在这些函数中我们需要实现权限检查 
def delete_user(iUserId): 
  """ delete the user with the given Id. This function is only accessable to users with administrator permissions """ 
def new_game(): 
  """ any logged in user can start a new game """ 
def premium_checkpoint(): 
  """ save the game progress, only accessable to premium members """

一种实现这些权限检查的方式是实现多个装饰器,比如:

def requires_admin(fn): 
  def ret_fn(*args,**kwargs): 
    lPermissions = get_permissions(current_user_id()) 
    if 'administrator' in lPermissions: 
      return fn(*args,**kwargs) 
    else: raise Exception("Not allowed") 
  return ret_fn
def requires_logged_in(fn): 
  def ret_fn(*args,**kwargs): 
    lPermissions = get_permissions(current_user_id()) 
    if 'logged_in' in lPermissions: 
      return fn(*args,**kwargs) 
    else: 
      raise Exception("Not allowed") 
    return ret_fn 
def requires_premium_member(fn): 
  def ret_fn(*args,**kwargs): 
    lPermissions = get_permissions(current_user_id()) 
    if 'premium_member' in lPermissions: 
      return fn(*args,**kwargs) 
    else: 
      raise Exception("Not allowed") 
    return ret_fn 
@requires_admin
def delete_user(iUserId): 
""" delete the user with the given Id. This function is only accessable to users with administrator permissions """
@requires_logged_in
def new_game(): 
""" any logged in user can start a new game """ @requires_premium_member
def premium_checkpoint(): 
""" save the game progress, only accessable to premium members """

但是,这太可怕了。这需要大量的复制粘贴,每个装饰器需要一个不同的名字,如果有任何关于权限检查的改变,每个装饰器都需要修改。就没有一个装饰器把以上三个装饰器的工作都干了的吗?

为了解决此问题,我们需要一个返回装饰器的函数:

def requires_permission(sPermission): 
  def decorator(fn): 
    def decorated(*args,**kwargs): 
      lPermissions = get_permissions(current_user_id()) 
      if sPermission in lPermissions: 
        return fn(*args,**kwargs)
      raise Exception("permission denied") 
    return decorated 
  return decorator
def get_permissions(iUserId): 
  # this is here so that the decorator doesn't throw NameErrors 
  return ['logged_in',]
def current_user_id(): 
  #ditto on the NameErrors 
  return 1
#and now we can decorate stuff... 
@requires_permission('administrator')
def delete_user(iUserId): 
""" delete the user with the given Id. This function is only accessible to users with administrator permissions """
@requires_permission('logged_in')
def new_game(): 
""" any logged in user can start a new game """ @requires_permission('premium_member')
def premium_checkpoint(): 
""" save the game progress, only accessable to premium members """

尝试一下调用delete_usernew namepremium_checkpoint然后看看发生了什么。
premium_checkpointdelete_user 产生了一个“permission denied”的异常,new_game执行正常。
下面是带参数装饰的一般形式,和例子的使用:

def outer_decorator(*outer_args,**outer_kwargs): 
  def decorator(fn): 
    def decorated(*args,**kwargs):
      do_something(*outer_args,**outer_kwargs) 
      return fn(*args,**kwargs) 
    return decorated 
  return decorator 
@outer_decorator(1,2,3)
def foo(a,b,c): 
  print(a) 
  print(b) 
  print(c)
foo()

等价于:

def decorator(fn): 
  def decorated(*args,**kwargs): 
    do_something(1,2,3) 
    return fn(*args,**kwargs) 
  return decorated 
return decorator 
@decorator
def foo(a,b,c): 
  print(a)
  print(b) 
  print(c)
foo()

类装饰器

装饰器不仅可以修饰函数,还可以对类进行装饰。比如说,我们有一个类,该类含有许多重要的方法,我们需要记录每一个方法执行的时间。我们可以使用上述的time_this装饰此类:

class ImportantStuff(object): 
@time_this 
def do_stuff_1(self): 
  pass
@time_this 
def do_stuff_2(self): 
  pass
@time_this 
def do_stuff_3(self): 
  pass

此方法可以运行正常。但是在该类中存在许多多余的代码,如果我们想建立更多的类方法并且遗忘了装饰其中的一个方法,如果我们不想装饰该类中的方法了,会发生什么样的情况呢?这可能会存在出现认为错误的空间,如果写成这样会更有好:

@time_all_class_methods
class ImportantStuff: 
  def do_stuff_1(self):
    pass
  def do_stuff_2(self):
    pass
  def do_stuff_3(self):
    pass

等价于:

class ImportantStuff: 
  def do_stuff_1(self):
    pass
  def do_stuff_2(self):
    pass
  def do_stuff_3(self):
    pass
ImportantStuff = time_all_class_methods(ImportantStuff)

那么time_all_class_methods是怎么工作的呢?
首先,我们需要采用一个类作为参数,然后返回一个类,我们也要知道返回的类的功能应该和原始类ImportantStuff功能一样。也就是说,我们仍然希望做重要的事情,我们希望记录下每个步骤发生的时间。我们写成这样:

def time_this(original_function): 
  print("decorating") 
  def new_function(*args,**kwargs): 
    print("starting timer") 
    import datetime 
    before = datetime.datetime.now() 
    x = original_function(*args,**kwargs) 
    after = datetime.datetime.now() 
    print("Elapsed Time = {0}".format(after-before)) 
    return x 
  return new_function
def time_all_class_methods(Cls): 
  class NewCls: 
    def __init__(self,*args,**kwargs): 
      self.oInstance = Cls(*args,**kwargs) 
    def __getattribute__(self,s): 
      try: 
        x = super(NewCls,self).__getattribute__(s) 
      except AttributeError: 
        pass 
      else: 
        return x 
      x = self.oInstance.__getattribute__(s) 
      if type(x) == type(self.__init__): 
        return time_this(x) 
      else: 
        return x 
    return NewCls
@time_all_class_methods
class Foo: 
  def a(self): 
    print("entering a") 
    import time 
    time.sleep(3) 
    print("exiting a")
oF = Foo()
oF.a()
# out:
decorating
starting timer
entering a
exiting a
Elapsed Time = 0:00:03.006767

总结

在此篇教程中,我们给大家展示了一些Python装饰器使用的技巧-我们介绍了怎么样把参数传递给装饰器,怎样装饰类。但是这仅仅是冰山一角。除了本文介绍的之外,还有其他好多装饰器的使用方法,我们甚至可以使用装饰器装饰装饰器(如果你有机会使用到它,这可能是一个做全面检查的好方法)。Python有一些内置的装饰器,比如:staticmethodclassmethod
阅读完本文还需要学习什么呢?通常是没有比我在文章中展示的装饰器更复杂的了,如果你有兴趣学习更多关于改变类功能的方法,我建议您阅读下继承和OOP设计原则。或者你可以试试阅读一下元类。

以上就是深入了解Python装饰器的高级用法的详细内容,更多关于Python装饰器的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
实例讲解Python设计模式编程之工厂方法模式的使用
Mar 02 Python
Django中login_required装饰器的深入介绍
Nov 24 Python
使用Python创建简单的HTTP服务器的方法步骤
Apr 26 Python
使用python对多个txt文件中的数据进行筛选的方法
Jul 10 Python
Python对接六大主流数据库(只需三步)
Jul 31 Python
Python使用scrapy爬取阳光热线问政平台过程解析
Aug 14 Python
python实现截取屏幕保存文件,删除N天前截图的例子
Aug 27 Python
Pytorch 神经网络—自定义数据集上实现教程
Jan 07 Python
初学者学习Python好还是Java好
May 26 Python
Python办公自动化之Excel(中)
May 24 Python
Windows安装Anaconda3的方法及使用过程详解
Jun 11 Python
关于 Python json中load和loads区别
Nov 07 Python
python高级特性简介
Aug 13 #Python
Pytest如何使用skip跳过执行测试
Aug 13 #Python
matplotlib基础绘图命令之bar的使用方法
Aug 13 #Python
Python logging模块原理解析及应用
Aug 13 #Python
matplotlib基础绘图命令之imshow的使用
Aug 13 #Python
使用jupyter notebook运行python和R的步骤
Aug 13 #Python
matplotlib基础绘图命令之errorbar的使用
Aug 13 #Python
You might like
浅谈PHP语法(1)
2006/10/09 PHP
PHP中使用Imagick操作PSD文件实例
2015/01/26 PHP
浅谈php处理后端&接口访问超时的解决方法
2016/10/29 PHP
jQuery第三课 修改元素属性及内容的代码
2010/03/14 Javascript
最佳JS代码编写的14条技巧
2011/01/09 Javascript
jQuery EasyUI API 中文文档 - PropertyGrid属性表格
2011/11/18 Javascript
JSON.stringify 语法实例讲解
2012/03/14 Javascript
JavaScript事件处理器中的event参数使用介绍
2013/05/24 Javascript
JavaScript中window、doucment、body的解释
2013/08/14 Javascript
javascript属性访问表达式用法分析
2015/04/25 Javascript
Javascript实现检测客户端类型代码封包
2015/12/03 Javascript
JS通过Cookie判断页面是否为首次打开
2016/02/05 Javascript
js使用Replace结合正则替换重复出现的字符串功能示例
2016/12/27 Javascript
解决vue2中使用axios http请求出现的问题
2018/03/05 Javascript
详解@Vue/Cli 3 Invalid Host header 错误解决办法
2019/01/02 Javascript
微信小程序使用蓝牙小插件
2019/09/23 Javascript
vue 中url 链接左边的小图标更改问题
2019/12/30 Javascript
微信小程序实现页面浮动导航
2020/01/08 Javascript
Python使用自带的ConfigParser模块读写ini配置文件
2016/06/26 Python
使用Pandas对数据进行筛选和排序的实现
2019/07/29 Python
基于Python实现船舶的MMSI的获取(推荐)
2019/10/21 Python
Pycharm创建项目时如何自动添加头部信息
2019/11/14 Python
利用matplotlib实现根据实时数据动态更新图形
2019/12/13 Python
CSS3实现同时执行倾斜和旋转的动画效果
2016/10/27 HTML / CSS
详解使用HTML5的classList属性操作CSS类
2017/10/13 HTML / CSS
Html5监听手机摇一摇事件的实现
2019/11/07 HTML / CSS
美国高端婴童品牌:Hanna Andersson
2016/10/30 全球购物
Melijoe美国官网:法国奢侈童装购物网站
2017/04/19 全球购物
GoPro摄像机美国官网:美国运动相机厂商
2018/07/03 全球购物
医学院学生求职简历的自我评价
2013/10/24 职场文书
毕业生怎样写好自荐信
2013/11/11 职场文书
宿舍违规用电检讨书
2014/02/16 职场文书
国旗下的讲话演讲稿
2014/05/08 职场文书
不会写演讲稿,快来看看这篇文章!
2019/08/06 职场文书
ORACLE查看当前账号的相关信息
2021/06/18 Oracle
基于Redis的List实现特价商品列表功能
2021/08/30 Redis