0

假设我有以下功能:

def simple_func():
    a = 4
    b = 5
    c = a * b
    print c

这是我跑步时得到的%debug simple_func()

NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
None
> <string>(1)<module>()

ipdb>

如果我进入n调试器,会向我吐出 20 并返回None.

这是跨函数、解释器、机器等发生的事情的简化版本。发生了什么?为什么我不能让我的任何调试器做我想做的事,而我只需要做一些非常简单的逐行单步调试?

4

1 回答 1

2

它看起来不适用于在会话debug中简单定义的函数。ipython它需要从文件中导入(即--breakpoint参数采用文件名和行)。

如果我创建一个文件test.py

In [9]: cat test.py
def simple_func():
    a = 4
    b = 5
    c = a * b
    print(c)

我可以:

In [10]: import test

In [11]: %debug --breakpoint test.py:1 test.simple_func()
Breakpoint 1 at /home/paul/mypy/test.py:1
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
> /home/paul/mypy/test.py(2)simple_func()
1     1 def simple_func():
----> 2     a = 4
      3     b = 5
      4     c = a * b
      5     print(c)

ipdb> n
> /home/paul/mypy/test.py(3)simple_func()
1     1 def simple_func():
      2     a = 4
----> 3     b = 5
      4     c = a * b
      5     print(c)

ipdb> n
> /home/paul/mypy/test.py(4)simple_func()
      2     a = 4
      3     b = 5
----> 4     c = a * b
      5     print(c)
      6 

ipdb> a,b
(4, 5)
ipdb> n
> /home/paul/mypy/test.py(5)simple_func()
      2     a = 4
      3     b = 5
      4     c = a * b
----> 5     print(c)
      6 

ipdb> c
20
ipdb> c
20
ipdb> q

可能还有其他使用方法,但这似乎是最简单、最直接的一种。我很少使用调试器。相反,我在 Ipython 中以交互方式测试代码片段,并在我的脚本中添加调试prints

于 2016-07-15T02:40:24.817 回答