How to take n steps (iterations) in Python debugger (PyCharm)?(如何在 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 步(迭代)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 调试器(PyCharm)中执行 n 步(迭代)?
基础教程推荐
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
