博客
关于我
理解和使用Python装饰器
阅读量:689 次
发布时间:2019-03-17

本文共 2503 字,大约阅读时间需要 8 分钟。

装饰器在 Python 中无处不在,功能强大。本篇介绍装饰器的原理和用法,力求通俗易懂。

我们从一个简单的例子开始,逐步展开。假设有一个函数,函数随便做点啥:

def foo():    print("do something.")

foobar 在英语中相当于汉语的张三、李四,意思就是随便给个名字。现在要给这个函数增加点功能,比如在函数调用前和调用后都打印一个输出提示。我们日常开发中经常有这类横向插入的需求,比如日志、权限检查等等。在 Java 中,实现这个功能可以用代理模式,但在 Python 中却异常简单,我们只需要另外一个嵌套的函数,如下:

def outer(func):           def inner():        print("before execution.")        func()        print("after execution.")    return inner # 无括号

然后这样使用:

if __name__ == "__main__":        proxy = outer(foo)    proxy()

程序输出:

before execution.do something.after execution.

在 Python 中,一切都是对象,函数也是对象。所以

proxy = outer(foo)

就是把 foo 函数作为参数传给 outer 函数,而 outer 函数呢,返回值为 inner 函数,所以 proxy 变量也参照inner 函数,再执行 proxy() 语句,就是调用 inner 函数,我们看内函数 inner 的代码,一共有三个语句:

print("before execution.")      # 打印输出func()                          # 执行 foo() 函数print("after execution.")       # 打印输出

Python 提供了装饰器语法糖·(@decorater_name),让我们使用这种嵌套的函数更加简单。下面是变更后的代码:

def outer(func):           def inner():        # 代码同上,省略    return inner @outerdef foo():    print("do something.")if __name__ == "__main__":        foo()

@outer 是一个语法糖,也就是我们所说的装饰器,这个 @outer 装饰器就是告诉 Python,在调用 foo 函数的时候,把它传给 outer 函数作为参数。outer 函数通过上述机制,既保证调用 foo 函数,也通过它自己的代码增强了 foo 的功能

上述代码为了说明机制,代码极其简化,一般化的装饰器代码是这样的:inner 函数有两个参数,以增加其灵活性。假设我们的需求实现一个 logger 装饰器,记录函数被调用的时间:

from datetime import datetimedef logger(func):    def wrapper(*args, **kwargs):        print ('[INFO] {}, function "{}" was called '.format(datetime.now(), func.__name__))        return func(*args, **kwargs)    return wrapper@loggerdef foo():    print("do something.")if __name__ == "__main__":        foo()

因为装饰器器外部函数需要以 function 作为参数,所以如果调用函数需要有信息需要传给装饰器,就需要再增加一层嵌套。

from datetime import datetimedef logger(msg):    def decorator(func):        def wrapper(*args, **kwargs):            print('[INFO] {}, "{}" was called with message "{}"'.format(                datetime.now(), func.__name__, msg))            return func(*args, **kwargs)        return wrapper    return decorator@logger("Maybe bored.")def foo(name):    print("do something, " + name)foo('Johnny')

装饰器也可以基于类来实现,要求装饰器类实现 __init__() 方法和 __call__() 方法。类装饰器实现 logger 的代码如下:

from datetime import datetimeclass logger(object):    def __init__(self, func):        self.func = func    def __call__(self, *args, **kwargs):        print ('[INFO] {}, function "{}" was called '.format(            datetime.now(), self.func.__name__))        return self.func(*args, **kwargs)@loggerdef foo():    print("do something.")if __name__ == "__main__":        foo()

转载地址:http://tbthz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>