gpt4 book ai didi

Python 相当于 C++ __LINE__

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:35 25 4
gpt4 key购买 nike

在 C++ 中调试的一个有用的打印是

std::cout << __LINE__ << std::endl;

当然你可以简单地打印一个带有行号的字符串,例如:

std::cout << "this is line 54" << std::endl;

但是当你四处移动它时它不会一直改变行号。Python 中是否有任何等效的宏?

最佳答案

没有宏,但你可以做

from inspect import currentframe, getframeinfo

print(getframeinfo(currentframe()).lineno)

在 Python 中获取当前行号。

“当前行号”将是评估 currentframe() 的行,仅供引用。

关于Python 相当于 C++ __LINE__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56762491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com