装饰器执行顺序问题

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/6/21 6:40
# @Author  : Derek

import time

def wrap1(func):
    def inner1(*args,**kwargs):
        print('wrap1.inner')
        start_time = time.time()
        func(*args,**kwargs)
        end_time = time.time()
        print('time of duration1 : %f'%(end_time-start_time))
        time.sleep(1)
    return inner1

def wrap2(func):
    def inner2(*args,**kwargs):
        print('wrap2.inner')
        start_time = time.time()
        func(*args,**kwargs)
        end_time = time.time()
        print('time of duration2 : %f'%(end_time-start_time))
        time.sleep(1)
    return inner2

@wrap1
@wrap2
def test_wrap(a):
    time.sleep(1)
    print('test_wrap print %d'%(a))

if __name__ == '__main__':
    test_wrap(1)

执行顺序是wrap1 >wrap2>test_wrap>wrap2>wrap1:

C:\Users\x\AppData\Local\Programs\Python\Python36\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.1\helpers\pydev\pydev_run_in_console.py" 4439 4440 C:/Users/x/PycharmProjects/test0621/wraptest.py
Running C:/Users/x/PycharmProjects/test0621/wraptest.py
wrap1.inner
wrap2.inner
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\\Users\\x\\PycharmProjects\\test0621', 'C:/Users/x/PycharmProjects/test0621'])
test_wrap print 1
time of duration2 : 1.002008
time of duration1 : 2.009012
PyDev console: starting.
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32

猜你喜欢

转载自www.cnblogs.com/xiaodebing/p/9206923.html
今日推荐