python使用装饰器记录函数执行次数
2018-04-19 21:32
267 查看
版权声明:本文为博主原创文章,转载请注明出处: https://blog.csdn.net/qq_31603575/article/details/80011287
def set_func(func): num = [0] # 闭包中外函数中的变量指向的引用不可变 def call_func(): func() num[0] += 1 print("执行次数",num[0]) return call_func # 待测试方法 @set_func def test(): pass test() test() test() # 执行次数 1 # 执行次数 2 # 执行次数 3
使用nonlocal 访问修改外部函数变量
def set_func(func): num = 0 # 闭包中外函数中的变量指向的引用不可变 def call_func(): func() nonlocal num # 使用nonlocal 访问修改外部函数变量 num += 1 print("执行次数",num) return call_func # 待测试方法 @set_func def test(): pass test() test() test() # 执行次数 1 # 执行次数 2 # 执行次数 3阅读更多
相关文章推荐
- Python: 使用装饰器“@”取得函数执行时间
- Python: 使用装饰器“@”取得函数执行时间
- Python使用装饰器和线程限制函数执行时间的方法
- python使用装饰器和线程限制函数执行时间的方法
- python用装饰器实现缓存函数执行结果
- python 8-1 如何使用多线程,Thread创建线程,执行函数赋值给target//类+函数放在run方法中执行
- Python学习23:函数装饰器的使用(方法重用)
- Python 进阶 —— 使用修饰器执行函数的参数检查
- python 使用装饰器并记录log
- python用装饰器实现缓存函数执行结果
- Python使用修饰器执行函数的参数检查功能示例
- PHP执行python脚本的passthru()函数使用及注意事项总结
- 如何使用定时器settimeout、setInterval执行能传递参数的函数
- Python里pycurl使用记录
- 如何使用定时器settimeout、setInterval执行能传递参数的函数
- PHP执行linux系统命令的常用函数使用说明
- PHP执行linux系统命令的常用函数使用说明
- Python在函数中使用*和**接收元组和列表
- C# Math.Rround函数使用记录
- 服务器控件customvalidator可以在前台执行脚本判断,不过要使用固定的函数