如何使用 pdb 跳过循环?
如何使用 pdb.set_trace()
跳过循环?
例如,
pdb.set_trace()
for i in range(5):
print(i)
print('Done!')
pdb
在循环之前提示。我输入一个命令。返回所有 1-5 值,然后我希望在执行 print('Done!')
之前再次收到 pdb
提示。
How can I skip over a loop using pdb.set_trace()
?
For example,
pdb.set_trace()
for i in range(5):
print(i)
print('Done!')
pdb
prompts before the loop. I input a command. All 1-5 values are returned and then I'd like to be prompted with pdb
again before the print('Done!')
executes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用
until
语句。转到循环的最后一行(使用
next
或n
),然后使用until
或unt
。这将带您进入循环之后的下一行。https://pymotw.com/3/pdb/index.html 有一个很好的解释
Try the
until
statement.Go to the last line of the loop (with
next
orn
) and then useuntil
orunt
. This will take you to the next line, right after the loop.https://pymotw.com/3/pdb/index.html has a good explanation
您应该在循环后设置一个断点(“break main.py:4”,假设上述行位于名为 main.py 的文件中),然后继续(“c”)。
You should set a breakpoint after the loop ("break main.py:4" presuming the above lines are in a file called main.py) and then continue ("c").
在接受的答案提到的链接中(https://pymotw.com/3/pdb/ ),我发现这一部分更有帮助:
以下是如何工作的示例:循环:
它可以让您免于两件事:必须创建额外的断点,并且必须导航到循环的结束(特别是当您可能已经进行迭代时,如果不重新运行调试器就无法进行迭代)。
这是关于
until
。顺便说一句,我使用pdb++
作为标准调试器的插件(因此是格式)但until
在两者中的工作方式相同。In the link mentioned by the accepted answer (https://pymotw.com/3/pdb/), I found this section somewhat more helpful:
Here's an example of how that can work re: loops:
It spares you from two things: having to create extra breakpoints, and having to navigate to the end of a loop (especially when you might have already iterated such that you wouldn't be able to without re-running the debugger).
Here's the Python docs on
until
. Btw I'm usingpdb++
as a drop-in for the standard debugger (hence the formatting) butuntil
works the same in both.您可以在循环后设置另一个断点并使用
c
跳转到该断点(调试时):You can set another breakpoint after the loop and jump to it (when debugging) with
c
:您可以使用
tbreak <循环后的行号>
。tbreak
是一个临时断点,首次命中时会被删除。You may use
tbreak <line number after loop>
.tbreak
is a temporary breakpoint that is removed when first hit.如果我理解正确的话。
一种可能的方法是:
一旦出现
pdb
提示。只需按n
(下一个)10 次即可退出循环。但是,我不知道如何退出 pdb 中的循环。
不过,您可以使用 r 来退出函数。
If I understood this correctly.
One possible way of doing this would be:
Once you get you
pdb
prompt . Just hitn
(next) 10 times to exit the loop.However, I am not aware of a way to exit a loop in
pdb
.You could use
r
to exit a function though.