当使用 python/ipython repl 进行开发和调试时,有时我想将函数中的所有局部变量转储到工作区以查看发生了什么。假设我有一个函数
def func():
a = "blablabla"
b = 1234
c = some_calculation_of_a_and_b(a,b)
dump_to_workspace(a,b,c) # is this possible?, or even more preferable:
dump_all_local_variables_to_workspace() # is this possible?
Run Code Online (Sandbox Code Playgroud)
我希望能够在 python/ipython 中运行它:
>>> func()
>>> print a
"blablabla"
>>> print b
1234
>>> print c
some_calculated_value
Run Code Online (Sandbox Code Playgroud)
我知道两种选择:(1)从函数返回变量[不好,因为我不想弄乱返回值],(2)将数据保存到磁盘上的文件中[不方便,因为它涉及可能具有大量数据的磁盘 I/O]。但大多数时候这些并不那么方便。有没有办法直接实现dump呢?
预先非常感谢!