开发者

Python: how to control namespace after an unhandled exception?

Is开发者_如何学运维 there any way to control which namespace you'll end up in after an unhandled exception?

For example, in an IPython prompt, run myscript.py on a file with these contents:

def spam():
    ham = "ham"
    crash = 1/0
    eggs = "eggs"

if __name__ == '__main__':
    foo = "foo"
    spam()

it crashes out with the zero division, and returns to IPython prompt with foo now in the namespace, but no ham. For some post mortem inspection, I want to return to the interpreter in the scope of the function spam() (i.e. ham would be available, eggs and foo would not).

We can access those things with pdb.set_trace() and re-running the code, but it would be great if there was a shortcut to step back a frame.


You can enable the automatic calling of pdb in the ipython prompt by typing %pdb.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜