<p>不能使用<code>continue</code>,因为调试器中的新语句需要<em>完整</em>并且在没有任何其他上下文的情况下有效;在编译</em>时,<code>continue</code>必须在循环构造<em>中给定。因此,即使调试器正在处理循环构造,也不能使用<code>!continue</code>(使用<code>!</code>来防止<code>pdb</code>解释命令)。</p>
<p>您可以使用<code>j[ump]</code>命令,<em>只要您稍后有一个<strong>语句跳转到</em>。如果要跳过的语句之后循环为空,则只能“倒带”:</p>
<pre><code>$ bin/python test.py
> /.../test.py(5)<module>()
-> print(str(i))
(Pdb) l
1 import pdb
2
3 for i in range(10):
4 pdb.set_trace()
5 -> print(str(i))
6
[EOF]
(Pdb) j 3
> /.../test.py(3)<module>()
-> for i in range(10):
</code></pre>
<p><code>j 3</code>跳到第3行,不跳过任何内容;将重新执行第3行,包括设置<code>range()</code>。您可以跳到第4行,但是<code>for</code>循环不会前进。</p>
<p>您需要在循环的末尾添加另一个语句,以便Python继续。该语句可以是<code>print()</code>或<code>pass</code>或任何不必改变状态的语句。您甚至可以使用<code>continue</code>作为最后一个语句。我用了<code>i</code>:</p>
<pre><code>for i in range(10):
pdb.set_trace()
print(str(i))
i # only here to jump to.
</code></pre>
<p>演示:</p>
<pre><code>$ bin/python test.py
> /.../test.py(5)<module>()
-> print(str(i))
(Pdb) l
1 import pdb
2
3 for i in range(10):
4 pdb.set_trace()
5 -> print(str(i))
6 i # only here to jump to.
7
[EOF]
(Pdb) j 6
> /.../test.py(6)<module>()
-> i # only here to jump to.
(Pdb) c
> /.../test.py(4)<module>()
-> pdb.set_trace()
(Pdb) s
> /.../test.py(5)<module>()
-> print(str(i))
(Pdb) j 6
> /.../test.py(6)<module>()
-> i # only here to jump to.
(Pdb) i
1
(Pdb) c
> /.../test.py(4)<module>()
-> pdb.set_trace()
(Pdb) s
> /.../test.py(5)<module>()
-> print(str(i))
(Pdb) j 6
> /.../test.py(6)<module>()
-> i # only here to jump to.
(Pdb) i
2
</code></pre>
<p>来自<a href="https://docs.python.org/2/library/pdb.html#debugger-commands" rel="noreferrer"><em>Debugger Commands</em></a>:</p>
<blockquote>
<p>j(ump) <em>lineno</em><br/>
Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run.</p>
<p>It should be noted that not all jumps are allowed — for instance it is not possible to jump into the middle of a <code>for</code> loop or out of a <code>finally</code> clause.</p>
</blockquote>