博客
关于我
理解和使用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/

你可能感兴趣的文章
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
nginx - thinkphp 如何实现url的rewrite
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx 301跳转
查看>>
nginx 403 forbidden
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>