#!/usr/bin/env python # -*- coding:utf-8 -*- # 作者: Apollo # 邮箱: [email protected] # 博客: http://www.zgerji.online/ import time flag = True def timmer_out(f): def timmer(func): def inner(*args, **kwargs): ‘‘‘执行函数前操作‘‘‘ if f: start_time = time.time() time.sleep(0.3) ret = func(*args, **kwargs) ‘‘‘执行函数后操作‘‘‘ end_time = time.time() print(‘执行效率%s‘ % (end_time - start_time)) return ret else: ret = func(*args, **kwargs) return ret return inner return timmer # timmer_out(flag),返回timmer @timmer_out(flag) # @timmer,就是你认识的装饰器,f1 = timmer(f1) def f1(): print(666) @timmer_out(flag) # f2 = timmer(f2) def f2(): print(777) f1() f2()
原文地址:https://www.cnblogs.com/apollo1616/p/9716622.html
时间: 2024-11-10 05:18:26