我正在使用 ipdb 和 yield。我注意到在将它与 ipdb 一起使用时,产量并不像预期的那样。
具体来说,这段代码在使用 ipdb 进行调试时(并按 'n' 键盘中的字符只是跳过 yield 命令而不是从函数返回)
def cats():
print(-1)
yield
for i in range(4):
print(i)
yield
import ipdb
ipdb.set_trace()
x = cats()
next(x)
next(x)
next(x)
如何解决?
最佳答案
ipdb 和 pdb 都需要在 yield
之后 声明才能在 cats() 中停止,但没有。有趣的是 pdb 会在返回时停止说:
def cats2():
if len(__file__) > 5:
import pdb; pdb.set_trace()
cats2()
老实说,在 pdb 及其衍生产品(如 ipdb)的背景下,我想不出解决方案。
环钻调试器 trepan3k (对于 python 3)和 trepan2不要遭受这个问题。他们对待 yield
的方式与 pdb 对待 return
的方式相同。正是为了这样的事情,修复了 pdb 无法处理的许多边缘情况,我编写了这些调试器。
关于python - 如何让 yield 在 Debug模式下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380092/