使用ipython和%pdb on访问调试会话中的变量

joo*_*oon 7 python ipython

我是ipython的新手,我正在尝试使用ipython来调试我的代码.我做了:

[1]: %pdb
Automatic pdb calling has been turned ON
Run Code Online (Sandbox Code Playgroud)

然后

In [2]: %run mycode.py
Run Code Online (Sandbox Code Playgroud)

在代码中,我有1/0所以它引发异常并自动进入调试会话.

ZeroDivisionError: float division

ipdb> variable
array([ 0.00704313, -1.34700666, -2.81474391])
Run Code Online (Sandbox Code Playgroud)

所以我可以访问变量.但是当我做以下事情时:

ipdb> b = variable
*** The specified object '= variable' is not a function or was not found along sys.path.
Run Code Online (Sandbox Code Playgroud)

但这有效:

ipdb> b = self.X
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 9

b用于设置断点.因此,无论b后面是什么,都应该是函数或行号.

如果键入,ipdb> help您将看到完整的命令列表(保留字).

您可以使用,比方说,xy作为变量使用:

ipdb> y = variable
Run Code Online (Sandbox Code Playgroud)

要么

ipdb> exec 'b = variable'
Run Code Online (Sandbox Code Playgroud)

  • 当然.只是角色b是数学对应物的自然选择.谢谢! (2认同)