在调试模式下从控制台跳出循环 [英] Break out of loop from console while in debug mode

查看:221
本文介绍了在调试模式下从控制台跳出循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在逐行调试 PyCharm 中的一些代码.该代码涉及一个很长的 for 循环,但出于调试目的,我不需要遍历整个过程.相反,我想在几次迭代后爆发.我正在尝试弄清楚如何在不修改我的代码以具有特殊调试说明的情况下执行此操作.

I'm debugging some code in PyCharm by stepping through line-by-line. The code involves a long for-loop, but for debugging purposes, I don't need to iterate through the whole thing. Instead, I'd like to break out after a few iterations. I'm trying to figure out how to do so without modifying my code to have special debugging instructions.

循环示例

indices = range(10000, 0, -1)
for i, val in enumerate(indices):
     print(val) # I can tell that it's working after a few iterations

我尝试的第一件事是在调试控制台中在 print(val) 行暂停时键入 break.这导致了语法错误

The first thing I tried was typing break in the debugging console when paused at the print(val) line. This resulted in a syntax error

语法错误:'break' 外循环

SyntaxError: 'break' outside loop

我还考虑过使用调试控制台行来修改我正在枚举的变量.例如输入

I also thought about using the debugging console line to modify the variable that I am enumerating over. For example, entering

indices = []

这对迭代器没有影响.显然,迭代器有一个独立的变量副本.

This has no effect on the iterator. Apparently, the iterator has an independent copy of the variable.

在另一种语言中,例如 Java,我会利用终止条件,但 python 循环没有终止条件.我可以将我的 for 循环重写为 while 循环,以便它具有明确的终止条件,但这会混淆代码.

In another language, Java for example, I would take advantage of the termination condition, but python loops don't have a termination condition. I could rewrite my for-loop as a while-loop so that it would have an explicit termination condition, but that would obfuscate the code.

另一种选择是添加特殊的调试指令.例如,

The other option would be to add special debugging instructions. For example,

mode = 'debug'
indices = range(10000, 0, -1)
for i, val in enumerate(indices):
     print(val) # I can tell that it's working after a few iterations
     if mode == 'debug' and i % 10:
         break

我不喜欢这样做,因为不可避免地,调试完成后会留下这些特殊指令之一并将所有内容弄糟.

I dislike doing this because inevitably, one of these special instructions gets left in after debugging is finished and mucks everything up.

我是否还有其他选择可以从调试控制台跳出循环或不修改代码?

Do I have any other options to break out of the loop from the debug console or without modifying the code?

推荐答案

我不喜欢这样做,因为不可避免地,调试完成后会留下这些特殊指令之一并将所有内容弄糟.

I dislike doing this because inevitably, one of these special instructions gets left in after debugging is finished and mucks everything up.

Python for 循环并非设计为 执行过程中改变,除了硬编码breakreturn或抛出StopIteration.(您正在调试,因此要么更改循环表达式列表中的范围,要么让它运行).原因是直接简单的优点超过了公开 for 循环计数器 - 请参阅 PEP 212.

Python for loops just aren't designed to be changed during execution, besides hard coding break, return or throwing StopIteration. (You are debugging, so either change the range in the loops expression list, or let it run through). The reason being the advantages of straightforward simplicity outweigh exposing the for loop counter - see PEP 212.

这篇关于在调试模式下从控制台跳出循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆