我想从被调用函数中检索 Python 的局部变量。有什么办法可以做到这一点吗?我意识到这对于大多数编程来说并不合适,但我基本上是在构建一个调试器。例如:
def show_locals():
# put something in here that shows local_1.
local_1 = 123
show_locals() # I want this to show local_1.
我在身体里放了什么show_locals
?如果我必须修改调用语句,我可以做的最小修改是什么?
Note:这必须在以下情况下起作用:show_locals
与其调用者位于不同的模块中。
如果您正在编写调试器,您将需要大量使用inspect module:
def show_callers_locals():
"""Print the local variables in the caller's frame."""
import inspect
frame = inspect.currentframe()
try:
print(frame.f_back.f_locals)
finally:
del frame
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)