Python 函数装饰器
装饰器软件开发的一种模式,即给一个对象增加一些新的功能,而且是动态的。UML关系图如下:Source类是被装饰类,...
扫描右侧二维码阅读全文
10
2021/11

Python 函数装饰器

装饰器

软件开发的一种模式,即给一个对象增加一些新的功能,而且是动态的。
UML关系图如下:
300975041_1566898979726_4AC5F9F4A06B5DD1C06484DE95111A39.jpg
Source类是被装饰类,Decorator类是装饰类
通过对装饰类Decorator对装饰类动态地添加一些功能,类似于spring框架中的AOP思想。

NowCoder


装饰器(Decorators)是 Python 的一个重要部分。简单地说:他们是修改其他函数的功能的函数。他们有助于让我们的代码更简短。

基于函数的装饰器

from functools import wraps


def a_new_decorator(a_func):
    """
    Python装饰器(decorator)在实现的时候,有一些细节需要被注意。
    例如,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变)。
    这样有时候会对程序造成一些不便,
    例如笔者想对flask框架中的一些函数添加自定义的decorator,
    添加后由于函数名和函数的doc发生了改变,对测试结果有一些影响。
    
    所以,Python的functools包中提供了一个叫wraps的decorator来消除这样的副作用。
    写一个decorator的时候,最好在实现之前加上functools的wrap,它能保留原有函数的名称和docstring
    """

    @wraps(a_func)
    def wrapTheFunction():
        print("I am doing some boring work before executing a_func()")
        a_func()
        print("I am doing some boring work after executing a_func()")

    return wrapTheFunction


@a_new_decorator
def a_function_requiring_decoration():
    """Hey yo! Decorate me!"""
    print("I am the function which needs some decoration to "
          "remove my foul smell")


print(a_function_requiring_decoration.__name__)
# Output: a_function_requiring_decoration

基于类的装饰器

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 opened_file:
                # 现在将日志打到指定的文件
                opened_file.write(log_string + '\n')
            # 现在,发送一个通知
            self.notify()
            return func(*args, **kwargs)

        return wrapped_function

    def notify(self):
        # logit只打日志,不做别的
        pass
    
@logit()
def myfunc1():
    pass
Last modification:November 10th, 2021 at 11:15 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment