def tips(func): def nei(a,b): print('start') func(a,b) print('stop') return nei 用装饰器来计算,函数耗时 def use_time(fun): def nei(*args, **kwargs): start = datetime.datetime.now() print('开始时间:',start) fun(*args, **kwargs) end = datetime.datetime.now() print('结束时间:', end) print('耗时:', end - start) return nei @use_time def p(num): for i in range(num): print(i) time.sleep(0.5) @tips def add(a,b): print(a+b) # raise None # add(1,4) add(2,9) p(5) 结果: 开始执行 test func is runing 执行完毕 start 11 stop 开始时间: 2022-06-06 19:44:23.170936 0 1 2 3 4 结束时间: 2022-06-06 19:44:25.696836 耗时: 0:00:02.525900