首页 > python 装饰器执行顺序

python 装饰器执行顺序

#!/usr/bin/python**加粗文字**
def deco_functionNeedDoc(func):
        if func.__doc__ == None:
                print func,"has no __doc__, it's a bad habit."
        else:
                print func,':',func.__doc__,'.'
        return func

@deco_functionNeedDoc
def f():
        print 'f() Do something'

@deco_functionNeedDoc
def g():
        'I have a __doc__'
        print 'g() Do something'
f()
g()

Actual Result:
<function f at 0x7f31e65d05f0> has no __doc__, it's a bad habit.
<function g at 0x7f31e65d0668> : I have a doc .
f() Do something
g() Do something

Expected Result:
<function f at 0x7f31e65d05f0> has no __doc__, it's a bad habit.
f() Do something
<function g at 0x7f31e65d0668> : I have a doc .
g() Do something
f()和g()被修饰后是如何执行的?

另外一段使用装饰器的代码

#-*- coding: UTF-8 -*-  
import time

def timeit(func):
    def wrapper():
        start = time.clock()
        func()
        end =time.clock()
        print 'used:', end - start
    return wrapper

@timeit
def foo():
    print 'in foo()'

foo()

Actual Result:

in foo()
used: 2.1e-05

这一段使用wrapper方法执行时先执行foo(),再执行wrapper()。
加了wrapper会有什么差别吗?


装饰器更像是“编译时”所执行的程序,即使在原函数还未装载入内存前就先为函数进行“装饰”工作,所以这里是先进行装饰,再运行的装饰后的函数。不然,你试试在最后不执行f()、g()同样可以执行装饰函数里面的代码


python是一种半编译半解释型运行行环境。首先会将源码编译为字节码,然后再执行这些字节码获得结果。

函数经过装饰器就被重新绑定了,也就是说

f = deco_functionNeedDoc(f)

所以装饰器应该是属于函数对象的一部分, 也就是说这一步是代码在编译时期,也就是编译为字节码的时候,就固定"写死"了,这个类似与函数的默认参数

def func(a = []):
    pass

都是在编译阶段就把就确定了。所以说咯,你的代码确定解释通啦, 在编译阶段先就执行装饰器重新绑定函数,然后执行阶段就直接运行f里面的东西。
PS:你这个装饰器的例子不太典型,可以找一些对原函数做了wrap的那种
以上是个人理解,共同讨论


修改之后的wrap装饰器函数, 其实也是先执行的timeit这个函数,只不过没有输出东西,而是直接返回了一个函数对象而已。比如你这样加一句:


#-*- coding: UTF-8 -*-  
import time

def timeit(func):
    def wrapper():
        start = time.clock()
        func()
        end =time.clock()
        print 'used:', end - start
    print 'I am timeit'
    return wrapper

@timeit
def foo():
    print 'in foo()'

foo()

输出就会变了,先输出I am timeit. 你再看看我上面说的,装饰器就是把函数重新绑定,返回另一个函数。 是在编译时期完成的, 再执行的时候,直接执行新函数。也就是你的wrapper

【热门文章】
【热门文章】