Python - print() - debuging -show 文件和行号



在python中使用print((时,是否可以在调用它的位置打印?因此,输出看起来像 php 中使用 xdebug 的var_dump。例如。我有脚本 D:\Something\script.py,在第 50 行有一个 print("sometest"(,所以输出将如下所示:

D:Somethinqscript.py:50 sometest

或者有任何模块可以实现这一点?在大型项目中,很难管理这些印刷品的来源。

因此,使用 python 脚本的文件名和行号中提供的答案,可以调用此函数代替 print((,并在每次输出之前打印行号:

from inspect import currentframe
def debug_print(arg):
frameinfo = currentframe()
print(frameinfo.f_back.f_lineno,":",arg)

最新更新