在 pdb 模式下,我经常想进入一个函数。这是一个说明我可能会做的事情的情况。鉴于代码:
def f(x):
print('doing important stuff..')
result = g(x) + 2
return result
def g(x):
print('some cool stuff going on here, as well')
0 / 0 # oops! a bug
return x + 5
Run Code Online (Sandbox Code Playgroud)
现在,假设我在print('doing important stuff...')和之间设置了一个断点result = g(x) + 2。所以现在,f(x)看起来像这样:
def f(x):
print('doing important stuff..')
__import__('pdb').set_trace() # or something similar..
result = g(x) + 2
return result
Run Code Online (Sandbox Code Playgroud)
然后,我调用该函数f(x)用x=5,期待得到一个结果12。当被调用时,我最终在f. 命中n会给我错误(在这种情况下是 ZeroDivisionError)。现在,我想以g(x)交互方式进入该函数以查看错误可能是什么。是否有可能在不“移动”断点g(x)并重新运行所有内容的情况下做到这一点?我只想g在仍处于 pdb 模式的同时在第一行输入该函数。
我已经搜索了以前的 SO 问题和答案 + 查找了文档,但仍然没有找到解决这种特殊情况的任何内容。