首页 > 编程语言 > 详细

python使用装饰器记录函数执行次数

时间:2021-07-09 23:18:13      阅读:20      评论:0      收藏:0      [点我收藏+]

‘‘‘
version: 0.2.0
Author: AbsoluteThree
Date: 2021-07-09 14:24:22
LastEditors: AbsoluteThree
LastEditTime: 2021-07-09 19:44:05
‘‘‘

1、利用装饰器,记录函数的运行次数

def func(f):
i=0
def func1():
nonlocal i
i+=1
f()
print(‘函数运行了%s次‘%i)
return func1
@func
def test():
pass
test()
test()

[Running] python -u "a:\LANGUAGE\py_text\a.py"
函数运行了1次
函数运行了2次

[Done] exited with code=0 in 0.117 seconds

python使用装饰器记录函数执行次数

原文:https://www.cnblogs.com/absolutethree/p/14992273.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!