您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将生成器对象转换为列表以进行调试

将生成器对象转换为列表以进行调试

只需调用list生成器。

lst = list(gen)
lst

请注意,这会影响生成器,生成器将不返回任何其他项目。

您也不能直接list在IPython中调用,因为它与列出代码行的命令冲突。

在此文件上测试:

def gen():
    yield 1
    yield 2
    yield 3
    yield 4
    yield 5
import ipdb
ipdb.set_trace()

g1 = gen()

text = "aha" + "bebe"

mylst = range(10, 20)

运行时:

$ python code.py 
> /home/javl/sand@R_577_2419@/so/debug/code.py(10)<module>()
      9 
---> 10 g1 = gen()
     11

ipdb> n
> /home/javl/sand@R_577_2419@/so/debug/code.py(12)<module>()
     11 
---> 12 text = "aha" + "bebe"
     13

ipdb> lst = list(g1)
ipdb> lst
[1, 2, 3, 4, 5]
ipdb> q
Exiting Debugger.

有调试器命令ppp这种意愿print,并prettyprint跟随他们任何表情。

因此,您可以按以下方式使用它:

$ python code.py 
> /home/javl/sand@R_577_2419@/so/debug/code.py(10)<module>()
      9 
---> 10 g1 = gen()
     11

ipdb> n
> /home/javl/sand@R_577_2419@/so/debug/code.py(12)<module>()
     11 
---> 12 text = "aha" + "bebe"
     13

ipdb> p list(g1)
[1, 2, 3, 4, 5]
ipdb> c

还有一个exec命令,通过在表达式前面加上调用,该命令!强制调试器将您的表达式当作Python。

ipdb> !list(g1)
[]

欲了解更多详情,请参阅help phelp pphelp exec在调试程序时。

ipdb> help exec
(!) statement
Execute the (one-line) statement in the context of
the current stack frame.
The exclamation point can be omitted unless the first word
of the statement resembles a debugger command.
To assign to a global variable you must always prefix the
command with a 'global' command, e.g.:
(Pdb) global list_options; list_options = ['-l']
其他 2022/1/1 18:31:45 有425人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶