Ada*_*žek 2 python printing debugging
在python中使用print()时,是否可以打印它被调用的地方?所以输出看起来像 php 中的 var_dump 和 xdebug。例如。我有脚本 D:\Something\script.py,在第 50 行,有一个打印(“sometest”),所以输出看起来像这样:
D:\Somethinq\script.py:50 sometest
Run Code Online (Sandbox Code Playgroud)
或者是否有任何模块可以实现这一点?在大型项目中,很难管理这些印刷品的来源。
因此,使用python 脚本的文件名和行号中提供的答案,可以调用此函数而不是 print(),并在每次输出之前打印行号:
from inspect import currentframe
def debug_print(arg):
frameinfo = currentframe()
print(frameinfo.f_back.f_lineno,":",arg)
Run Code Online (Sandbox Code Playgroud)