如何使yield在调试模式下工作? [英] How to make yield work in debug mode?

查看:154
本文介绍了如何使yield在调试模式下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ipdb和yield.我注意到与ipdb配合使用时,产量不符合预期.

I am working with ipdb and yield. I noticed the yield does not act as expected when using it with ipdb.

具体来说,使用ipdb调试此代码(并按"n" 键盘上的charcter只是跳过了yield命令,而不是从函数中返回)

Specifically, this code when being debugged with ipdb (and pressing the 'n' charcter in the key board simply skips the yield command instead of returning from the function)

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()内部em>,没有任何东西.有趣的是,尽管pdb会在返回时停止:

Both ipdb and pdb need a statement after the yield for them to stop on inside cats() and there is none. Interesting though that pdb will stop on the return in say:

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

老实说,我无法在pdb及其派生类(例如ipdb)的上下文中想到解决方案.

I honestly can't think of a solution for this in the context of pdb its derivatives like ipdb.

trepan调试器 trepan3k (适用于python 3)和

The trepan debuggers trepan3k (for python 3) and trepan2 do not suffer this problem. They treat yield the same way pdb treats return. And it is for things like this, fixing a lot of edge cases that pdb just doesn't handle, that I wrote these debuggers.

这篇关于如何使yield在调试模式下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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