• 装饰器执行顺序问题


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

  • 相关阅读:
    heapq of python
    array of python
    Unittest of Python
    事件驱动型工作流 vs 引擎型工作流
    airflow
    WPF 调试触发器
    WPF 使用Popup和TreeView实现树状下拉框
    Oracle : ORA 00933: SQL command not properly ended
    PostgreSQL && PostGIS
    基于ArcGIS开发3D立方体空间关系判断
  • 原文地址:https://www.cnblogs.com/xiaodebing/p/9206923.html
Copyright © 2020-2023  润新知