码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器执行顺序问题

时间:2018-06-21 11:50:14      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:com   1.0   time   python3   rap   version   app   oca   pycharm   

#!/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

装饰器执行顺序问题

标签:com   1.0   time   python3   rap   version   app   oca   pycharm   

原文地址:https://www.cnblogs.com/xiaodebing/p/9206923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!