Python 使用类写装饰器的小技巧


Posted in Python onSeptember 30, 2018

最近学到了一个有趣的装饰器写法,就记录一下。

装饰器是一个返回函数的函数。写一个装饰器,除了最常见的在函数中定义函数以外,Python还允许使用类来定义一个装饰器。

1、用类写装饰器

下面用常见的写法实现了一个缓存装饰器。

def cache(func):
  data = {}
  def wrapper(*args, **kwargs):
    key = f'{func.__name__}-{str(args)}-{str(kwargs)})'
    if key in data:
      result = data.get(key)
      print('cached')
    else:
      result = func(*args, **kwargs)
      data[key] = result
      print('calculated')
    return result
  return wrapper

看看缓存的效果。

@cache
def rectangle_area(length, width):
  return length * width
rectangle_area(2, 3)
# calculated
# 6
rectangle_area(2, 3)
# cached
# 6

装饰器的@cache是一个语法糖,相当于func = cache(func),如果这里的cache不是一个函数,而是一个类又会怎样呢?定义一个类class Cache, 那么调用func = Cache(func)会得到一个对象,这时返回的func其实是Cache的对象。定义__call__方法可以将类的实例变成可调用对象,可以像调用函数一样调用对象。然后在__call__方法里调用原本的func函数就能实现装饰器了。所以Cache类也能当作装饰器使用,并且能以@Cache的形式使用。

接下来把cache函数改写为Cache类:

class Cache:
  def __init__(self, func):
    self.func = func
    self.data = {}
  def __call__(self, *args, **kwargs):
    func = self.func
    data = self.data
    key = f'{func.__name__}-{str(args)}-{str(kwargs)})'
    if key in data:
      result = data.get(key)
      print('cached')
    else:
      result = func(*args, **kwargs)
      data[key] = result
      print('calculated')
    return result

再看看缓存结果,效果一样。

@Cache
def rectangle_area(length, width):
  return length * width
rectangle_area(2, 3)
# calculated
# 6
rectangle_area(2, 3)
# cached
# 6

2、装饰类的方法

装饰器不止能装饰函数,也经常用来装饰类的方法,但是我发现用类写的装饰器不能直接用在装饰类的方法上。(有点绕…)

先看看函数写的装饰器如何装饰类的方法。

class Rectangle:
  def __init__(self, length, width):
    self.length = length
    self.width = width
  @cache
  def area(self):
    return self.length * self.width
r = Rectangle(2, 3)
r.area()
# calculated
# 6
r.area()
# cached
# 6

但是如果直接换成Cache类会报错,这个错误的原因是area被装饰后变成了类的一个属性,而不是方法。

class Rectangle:
  def __init__(self, length, width):
    self.length = length
    self.width = width
  @Cache
  def area(self):
    return self.length * self.width
r = Rectangle(2, 3)
r.area()
# TypeError: area() missing 1 required positional argument: 'self'
Rectangle.area
# <__main__.Cache object at 0x0000012D8E7A6D30>
r.area
# <__main__.Cache object at 0x0000012D8E7A6D30>

回头再来看看没有装饰器的情况,Python在实例化对象后把函数变成了方法。

class Rectangle:
  def __init__(self, length, width):
    self.length = length
    self.width = width

  def area(self):
    return self.length * self.width

Rectangle.area
# <function Rectangle.area at 0x0000012D8E7B28C8>
r = Rectangle(2, 3)
r.area
# <bound method Rectangle.area of <__main__.Rectangle object

因此解决办法很简单,要用类写的装饰器来装饰类的方法,只需要把可调用对象包装成函数就行。

# 定义一个简单的装饰器,什么也不做,仅仅是把可调用对象包装成函数
def method(call):
  def wrapper(*args, **kwargs):
    return call(*args, **kwargs)
  return wrapper
class Rectangle:
  def __init__(self, length, width):
    self.length = length
    self.width = width
  @method
  @Cache
  def area(self):
    return self.length * self.width
r = Rectangle(2, 3)
r.area()
# calculated
# 6
r.area()
# cached
# 6

或者用@property还能直接把方法变成属性。

class Rectangle:
  def __init__(self, length, width):
    self.length = length
    self.width = width
  @property
  @Cache
  def area(self):
    return self.length * self.width
r = Rectangle(2, 3)
r.area
# calculated
# 6
r.area
# cached
# 6

总结

用类写装饰器并非什么特别的技巧,一般情况下确实没必要这么写,不过这样就可以用一些类的特性来写装饰器,比如类的继承,也算是提供了另一种思路吧。

Python 相关文章推荐
python使用递归解决全排列数字示例
Feb 11 Python
python通过邮件服务器端口发送邮件的方法
Apr 30 Python
Python中集合的内建函数和内建方法学习教程
Aug 19 Python
python二分查找算法的递归实现方法
May 12 Python
Python编程之微信推送模板消息功能示例
Aug 21 Python
Python WXPY实现微信监控报警功能的代码
Oct 20 Python
python+opencv+caffe+摄像头做目标检测的实例代码
Aug 03 Python
Python第三方库face_recognition在windows上的安装过程
May 03 Python
解决python replace函数替换无效问题
Jan 18 Python
opencv python如何实现图像二值化
Feb 03 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
Feb 18 Python
基于pytorch中的Sequential用法说明
Jun 24 Python
浅谈django三种缓存模式的使用及注意点
Sep 30 #Python
使用Python实现租车计费系统的两种方法
Sep 29 #Python
Python实现App自动签到领取积分功能
Sep 29 #Python
10个Python小技巧你值得拥有
Sep 29 #Python
实例分析python3实现并发访问水平切分表
Sep 29 #Python
3个用于数据科学的顶级Python库
Sep 29 #Python
使用Python机器学习降低静态日志噪声
Sep 29 #Python
You might like
两种php去除二维数组的重复项方法
2015/11/04 PHP
PHP传值到不同页面的三种常见方式及php和html之间传值问题
2015/11/19 PHP
PHP数据库操作Helper类完整实例
2016/05/11 PHP
php使用number_format函数截取小数的方法分析
2016/05/27 PHP
Yii2 加载css、js 载静态资源的方法
2017/03/10 PHP
PHP PDOStatement::getColumnMeta讲解
2019/02/01 PHP
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
2020/02/10 PHP
用JavaScript事件串连执行多个处理过程的方法
2007/03/09 Javascript
使用js操作css实现js改变背景图片示例
2014/03/10 Javascript
JavaScript获取flash对象与网上的有所不同
2014/04/21 Javascript
jQuery旋转木马式幻灯片轮播特效
2015/12/04 Javascript
JavaScrip常见的一些算法总结
2015/12/28 Javascript
AngularJS入门之动画
2016/07/27 Javascript
Angular @HostBinding()和@HostListener()用法
2018/03/05 Javascript
微信小程序仿朋友圈发布动态功能
2018/07/15 Javascript
解决iview多表头动态更改列元素发生的错误的方法
2018/11/02 Javascript
[01:19]DOTA2城市挑战赛报名开始 开启你的城市传奇
2018/03/23 DOTA
python内存管理分析
2015/04/08 Python
python学习数据结构实例代码
2015/05/11 Python
Python中字典的基础知识归纳小结
2015/08/19 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
2017/02/16 Python
python中将一个全部为int的list 转化为str的list方法
2018/04/09 Python
python定时关机小脚本
2018/06/20 Python
pygame游戏之旅 添加游戏介绍
2018/11/20 Python
如何使用pyinstaller打包32位的exe程序
2019/05/26 Python
Python装饰器用法与知识点小结
2020/03/09 Python
一款css实现的鼠标经过按钮的特效
2014/09/11 HTML / CSS
常用UNIX 命令(Linux的常用命令)
2013/07/10 面试题
药剂学专业应届生自荐信
2013/09/29 职场文书
儿科护士自我鉴定
2013/10/14 职场文书
公司面试感谢信
2014/02/01 职场文书
优秀班组长事迹
2014/05/31 职场文书
不尊敬老师的检讨书
2014/12/21 职场文书
企业管理制度设计时要注意的几种“常见病”!
2019/04/19 职场文书
聊聊基于pytorch实现Resnet对本地数据集的训练问题
2022/03/25 Python
vue特效之翻牌动画
2022/04/20 Vue.js