如何在 Python 调试器(PyCharm)中执行 n 步(迭代)? [英] How to take n steps (iterations) in Python debugger (PyCharm)?

查看:30
本文介绍了如何在 Python 调试器(PyCharm)中执行 n 步(迭代)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Python 调试器中有一个断点.我正在使用 PyCharm.我想迭代让我们说 100 次以达到我要调试的点.

I have a breakpoint in my Python debugger. I am using PyCharm. I want to iterate lets say 100 times to reach the point where I want to debug.

现在我可以按 100 次 Resume Program,但是有没有办法只执行一个命令在断点上运行 n 次.

Now I can press 100 x times Resume Program, but is there a way to just execute a command to run n times over the breakpoint.

推荐答案

可以在条件断点中使用函数来计算迭代次数,例如:

You can use a function in a conditional breakpoint to count iterations, take for example:

条件断点可以调用一个函数,该函数除了返回一个布尔值外,还计算循环迭代的次数.

The conditional breakpoint can call a function which in addition to returning a boolean, counts the number of loop iterations.

def your_counter(stop):
    global count
    count = count + 1
    if stop == count:
        # count = 0 for periodic break
        return True
    else:
        return False

显示的解决方案适用于单行条件可能不实用和/或 循环计数器需要在外部实现.由于断点条件是程序化的,您可以实现它以定期中断,或根据您想要应用的任何系列/频率标准.

The solution shown is for cases when a one-liner condition might not be practical, and/or when a loop counter needs to be implemented externally. Since the breakpoint condition is programmatic you can implement it to break periodically, or on any series/frequency criteria you want to apply.

在您完成分步调试"后,自定义条件将在您想要的确切迭代处中断.要么按恢复、停止、运行到光标",要么禁用断点右键单击它(实际上这会让你跳出循环).

The custom condition would break at the exact iteration you want, after you're done "step debugging" either press resume, stop, "run to cursor", or disable the breakpoint right-clicking on it (in practice this gets you out of the loop).

您还可以在调试过程中通过在变量监视"中进行编辑来更改任何变量的值.

You can also change the value of any variable in the middle of debugging by editing in "variable watches".

这篇关于如何在 Python 调试器(PyCharm)中执行 n 步(迭代)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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