zoukankan      html  css  js  c++  java
  • 装饰器执行顺序问题

    #!/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:UsersxAppDataLocalProgramsPythonPython36python.exe "C:Program FilesJetBrainsPyCharm Community Edition 2017.3.1helperspydevpydev_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

  • 相关阅读:
    hdoj 2586 How far away?(最近公共祖先)
    poj 1330 A-Nearest Common Ancestors
    心形图
    B1928 日期差值
    B1022 D进制的A+B
    B1009 说反话
    hihocoder 1498 签到
    51Nod 1082 与7无关的数
    51Nod 1015 水仙花数
    51Nod 1283 最小周长
  • 原文地址:https://www.cnblogs.com/xiaodebing/p/9206923.html
Copyright © 2011-2022 走看看