Loading

python装饰器

import time


def cost_time(func):
    def inner():
        t1 = time.perf_counter()
        func()
        t2 = time.perf_counter()
        print(t2 - t1)

    return inner


@cost_time
def step_two():
    for i in range(10000):
        print(i)


def step_one():
    t1 = time.perf_counter()
    for i in range(10000):
        print(i)
    t2 = time.perf_counter()
    print(t2 - t1)


if __name__ == '__main__':
    step_one()
    step_two()

装饰器常用用法:

引入日志

函数执行时间统计

执行函数前预处理

执行函数前的清理功能

权限校验

等等

posted @ 2022-07-10 18:27  上官飞鸿  阅读(46)  评论(0)    收藏  举报