Python 装饰器

装饰器本质上是一个 Python 函数或类,它会接受一个callable对象作为参数,然后再返回一个callable对象作为返回值。装饰器可以在不修改原有对象代码的基础上,为对象添加额外的功能。它经常用于有切面需求的场景,比如:插入日志、性能测试、事务处理、缓存、权限校验等场景,有了装饰器,我们就可以抽象出大量与函数功能本身无关的雷同代码到装饰器中进行重用。

简单装饰器

下面是一个简单的装饰器例子:

def log(func):  
    def wrapper(*args, **kwargs):
        print('The following function name is [%s]' % func.__name__)
        return func(*args, **kwargs)
    
    return wrapper

@log    # 无()
def test():
    print('Hi, I am test' )

test()

# Output>>>
The following function name is [test]
Hi, I am test

上例中,log 就是一个装饰器,它接收一个函数(func)作为参数,同时也返回一个函数(wrapper)。从运行结果可以发现,test 函数不需要做任何修改,只需在函数定义的地方加上装饰器,@ 符号是装饰器的语法糖,它放在函数开始定义的地方,这样就可以省略最后一步再次赋值的操作。函数调用的时候还是和以前一样,如果我们还有其他的类似函数,我们可以继续调用装饰器来修饰函数,而不用重复修改函数或者增加新的封装。这样,我们不但为对象添加了额外的功能,还提高了代码的可重复利用性。

装饰器在 Python 中使用如此方便,主要归因于 Python 中一切皆对象的思想,函数也能像普通的对象一样,能作为参数传递给其他函数,可以被赋值给其他变量,可以作为返回值,可以被定义在另外一个函数内,以及闭包结构所拥有的极大威力。

继续看下面的例子:

def log(func):
    def wrapper():
        '''This is decorator'''
        print('The following function name is [%s]' % func.__name__)
        return func(*args, **kwargs)

    return wrapper

@log
def test():
    '''This is test''' 
    print(test.__doc__)
    print('Hi, %s' % test.__name__)

test()

# Output>>>
The following function name is [test]
This is decorator
Hi, wrapper

从运行结果中看到,使用装饰器后,test函数的函数名和注释文档等元信息被wrapper的元信息替换了,这显然不是我们想要看到的。Python提供给我们一个functools.wraps函数来解决这个问题,对上面的代码进行修改:

import functools 

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

def log(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        '''This is decorator'''
        print('>>>The following function name is %s' % func.__name__)
        return func(*args, **kwargs)

    return wrapper

@log    
def test():
    '''This is test'''
    print(test.__doc__)
    print('Hi, %s' % test.__name__)

test()

# Output>>>
The following function name is test
This is test
Hi, test

从运行结果来看,现在已经达到了我们所期望的结果。

带参数的装饰器

在上面的代码中使用的@wraps装饰器,它可以像普通函数一样接受参数,下面来实现一个带参数的装饰器,用来输出日志文件。

from datetime import datetime
from functools import wraps
import logging

def logit(log_level='info'):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            log_file = datetime.now().strftime('%Y%m%d') + '.log'
            log_string = 'Current function is %s' % func.__name__
            if log_level == 'warning':
                logging.warning('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(log_level))
            elif log_level == 'info':
                logging.info('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(log_level))
            elif log_level == 'error':
                logging.debug('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(log_level))
            else:
                raise ValueError('log level is illegal.')
            return func(*args, **kwargs)

        return wrapper

    return decorator


@logit(log_level='warning')
def test1():
    print('I am %s' % test1.__name__)


@logit(log_level='error')
def test2():
    print('I am %s' % test2.__name__)


test1()
test2()

运行代码,会生成一个名称为"当前日期.log"的文件,文件内容如下:

再举个例子,现在想要定义一个参数,来表示装饰器内部函数被执行的次数,那么就可以写成下面的形式:

import functools

def repeat(num):
    def deco(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            for i in range(num):
                func(*args, **kwargs)

        return wrapper

    return deco

类装饰器

绝大多数装饰器都是基于函数和闭包实现的,但这并非制造装饰器的唯一方式。事实上,Python 对某个对象是否能通过装饰器( @decorator)形式使用只有一个要求:decorator 必须是一个“可被调用(callable)的对象。
函数自然是“可被调用”的对象。但除了函数外,我们也可以让任何一个类(class)变得“可被调用”(callable)。办法很简单,只要重写类的__call__方法即可。相比函数装饰器,类装饰器具有灵活度大、高内聚、封装性等优点。使在下面的代码中,以类的方式来重新构建logit.

import logging
from datetime import datetime
from functools import wraps

class logit:
    def __init__(self, log_level='info'):
        self.log_level = log_level
    def __call__(self, func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            log_file = datetime.now().strftime('%Y%m%d') + '.log'
            log_string = 'Current function is %s' % func.__name__
            if self.log_level == 'warning':
                logging.warning('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(self.log_level))
            elif self.log_level == 'info':
                logging.info('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(self.log_level))
            elif self.log_level == 'error':
                logging.debug('%s is running' % func.__name__)
                with open(log_file, 'a') as f:
                    f.write((str(datetime.now()) + '\n' + '[{}]' + log_string + '\n').format(self.log_level))
            else:
                raise ValueError('log level is illegal.')
            return func(*args, **kwargs)

        return wrapper

@logit()
def test1():
    print('I am %s' % test1.__name__)

@logit(log_level='error')
def test2():
    print('I am %s' % test2.__name__)

test1()
test2()

上述实现方法比函数嵌套的方法更加整洁,并且可以方便的使用继承的场景。

装饰器的嵌套

Python 也支持多个装饰器,比如下面的例子:

import functools

def deco1(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("execute deco1")
        return func(*args, **kwargs)
    return wrapper

def deco2(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("execute deco2")
        return func(*args, **kwargs)
    return wrapper

def deco3(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("execute deco3")
        return func(*args, **kwargs)
    return wrapper

@deco3
@deco2
@deco1
def test(message):
    print(message)

test("Hi~")

它的执行顺序是从里到外(从下到上),下一个装饰方法的返回值,接收的是上一个装饰方法的返回结果,上面的语句也等效于下面这行代码:

deco3(deco2(deco1(test("Hi~"))))

输出的内容为:

execute deco3
execute deco2
execute deco1
Hi~

[To be continued....]

参考文档

  1. Python装饰器廖雪峰

  2. Python 装饰器执行顺序迷思 , Nisen

  3. 详解Python的装饰器一试就错

  4. Python函数装饰器,菜鸟教程

  5. Python装饰器的另类用法一试就错

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 部分细节自己改了点,也加了点自己例子,基本上属于转载。转载出处:https://my.oschina.net/le...
    洛克黄瓜阅读 2,126评论 0 3
  • 每个人都有的内裤主要功能是用来遮羞,但是到了冬天它没法为我们防风御寒,咋办?我们想到的一个办法就是把内裤改造一下,...
    chen_000阅读 1,405评论 0 3
  • 谈装饰器前,还要先要明白一件事,Python 中的函数和 Java、C++不太一样,Python 中的函数可以像普...
    明日孤风寒阅读 323评论 0 0
  • 一、史前故事 先看一个简单例子,实际可能会复杂很多: 现在有一个新的需求,希望可以记录下函数的执行日志,于是在代码...
    DevinZhang阅读 904评论 4 22
  • 别再把伤害说成教育 16年有段视频相当火爆​,是邹市明太太冉莹颖“教育”儿子的视频,得到广大人民群众的一致好评。 ...
    李念真阅读 577评论 0 0

友情链接更多精彩内容