python 的装饰器以前不咋会,挺好用,能写,但是原理真是不咋知道。


import datetime
import time


def catch_one(*zzz):
    print('---catch_one---1', *zzz)
    def function_name(fun):
        print('---catch_one---2', fun)
        time.sleep(1)
        def operate(*args, **kwargs):
            print('---catch_one---3', *args, **kwargs)
            print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
            a =  fun(*args, **kwargs)
            print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
            return a
        print('---catch_one---4')
        return operate
    print('---catch_one---5')
    return function_name


def catch_two(*aa):
    print('catch_two---1', aa)
    def function_two(func):
        print('catch_two---2', func)
        def par(*args, **kwargs):
            print('catch_two start...3', *args, **kwargs)
            func(*args, **kwargs)
            print('catch_two end...4')
        print('catch_two---5')
        return par
    print('catch_two---6')
    return function_two


@catch_two('aaaaaaaaaaaaa')
@catch('111')
def test(qq):
    time.sleep(qq)

def test_2(qq):
    time.sleep(qq)



↙↙↙阅读原文可查看相关链接,并与作者交流