PySnooper¶
Github:https://github.com/lotapp/PySnooper
pip install pysnooper
使用:分析整个代码
@pysnooper.snoop() def avg(args): return sum(args) / len(args)
PS:调试代码片段
with pysnooper.snoop(): code
In [1]:
import functools import pysnooper # 分析整个代码 @pysnooper.snoop() def sum(args): return functools.reduce(lambda x, y: x + y, args) def avg(args): # return sum(args) / len(args) sum_num = sum(args) # 分析片段代码 with pysnooper.snoop(): avg_num = sum_num / len(args) return avg_num
In [2]:
# 求和 sum(list(range(1, 101)))
Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] 12:21:19.846556 call 6 def sum(args): 12:21:19.847553 line 7 return functools.reduce(lambda x, y: x + y, args) 12:21:19.847553 return 7 return functools.reduce(lambda x, y: x + y, args) Return value:.. 5050
Out[2]:
5050
In [3]:
# 平均值 avg(list(range(1, 101)))
Starting var:.. args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] 12:21:19.940523 call 6 def sum(args): 12:21:19.941526 line 7 return functools.reduce(lambda x, y: x + y, args) 12:21:19.941526 return 7 return functools.reduce(lambda x, y: x + y, args) Return value:.. 5050 New var:....... args = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] New var:....... sum_num = 5050 12:21:19.942524 line 14 avg_num = sum_num / len(args)
Out[3]:
50.5
原文地址:https://www.cnblogs.com/dotnetcrazy/p/10848257.html
时间: 2024-11-09 01:51:03