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

2021/7/9 20:10:38

本文主要是介绍python使用装饰器记录函数执行次数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

'''
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使用装饰器记录函数执行次数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程