【问题标题】:How to make yield work in debug mode?如何使产量在调试模式下工作?
【发布时间】:2018-05-02 23:14:37
【问题描述】:

我正在使用 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)

如何解决?

【问题讨论】:

    标签: python python-3.x yield ipdb


    【解决方案1】:

    ipdb 和 pdb 都需要一个语句 yield 之后才能停止在cats() 内,但没有。有趣的是,pdb 会在返回时停止说:

    def cats2():
            if len(__file__) > 5:
                import pdb; pdb.set_trace()
    cats2()
    

    老实说,在 pdb 及其衍生产品(如 ipdb)的上下文中,我想不出一个解决方案。

    trepan 调试器 trepan3k(用于 python 3)和 trepan2 不会遇到这个问题。他们对待 yield 的方式与 pdb 对待 return 的方式相同。正是为了这样的事情,修复了许多 pdb 无法处理的边缘情况,我编写了这些调试器。

    【讨论】:

    • 猫是一回事
    猜你喜欢
    • 2011-11-14
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多