首页 > 编程语言 > 详细

Python装饰器

时间:2019-11-06 19:20:24      阅读:63      评论:0      收藏:0      [点我收藏+]

本文来介绍一下Python的函数装饰器,类装饰器以后另开一文讲。

装饰器可以看做是Python中的一个语法糖,基本的写法示例如下:

def decorator(func):
    return func

@decorator
def function():
    print "666"

decorator即是function的装饰器,在function函数前面加上@decorator的前缀相当于:

function = decorator(function)

所以之后对function的调用实际上都是在调用decorator。

以上只是最最简单的写法,一般装饰器会在函数内部定义一些自己的操作,相当于“装饰”了原本的函数。例如:

import time
def decorator(func):
    def clocked(*args):
        t0 = time.time()
        result = func(*args)
        elapsed = time.time() - t0
        name = func.__name__
        print elapsed, name, result
        return result
    return clocked

@decorator
def function():
    print "666"

function()
print function

输出如下:

666
0.0 function None
<function clocked at 0x03270A30>

可以看到:

1.该装饰器的作用是输出所装饰函数的运行时间,和函数名以及返回值

2.通过输出function,可以发现此时它已经被替换成了clocked,这与前面讲的“function = decorator(function)”是吻合的。

另外关于装饰器要注意的一点是:装饰器是在加载该模块的时候执行的。举个例子,假设有如下代码:

def decorator(func):
    print "7777"
    return func

@decorator
def function():
    print "666"

if __name__ == __main__:
    print "start!"
    function()

运行该py文件后输出如下:

7777
start!
666

可以看到,“777”出现在“start!”之前,这说明装饰器是在实际运行该模块之前输出的。

装饰器是可以累加的,

def decorator1(func):
    print "666"
    return func
    
def decorator2(func):
    print "777"
    return func

@decorator2
@decorator1
def function():
    print "88"

等同于

function = decorator2(decorator1(function))

 

 如果我们想给装饰器传入参数,该如何做呢?这就需要我们创建一个装饰器工厂函数,把参数传给它,返回一个装饰器,然后再把它应用到要装饰的函数上。具体示例如下:

import time
def decorator(is_active=True):
    def dec1(func):
        if is_active:
            print "888"
        else:
            print "777"
        return func
    return dec1

@decorator(True)
def function():
    print "666"

dec1才是真正的装饰器(它的参数是func),decorator可以理解为一个装饰器工厂,它返回相应的装饰器函数。

 这次装饰器就讲解到这里,之后有时间会再讲解一下类装饰器。



 

Python装饰器

原文:https://www.cnblogs.com/wickedpriest/p/11806776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!