Sea*_*123 90 python generator ipdb
当我使用IPython在Python中进行调试时,我有时会遇到一个断点,我想检查一个当前是生成器的变量.我能想到的最简单的方法就是将它转换为一个列表,但我不清楚在一行中这样做的简单方法是什么ipdb,因为我对Python很新.
Jan*_*sky 156
只需打电话list给发电机.
lst = list(gen)
lst
Run Code Online (Sandbox Code Playgroud)
请注意,这会影响不会返回任何其他项目的生成器.
您也无法直接调用listIPython,因为它与列出代码行的命令冲突.
测试此文件:
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)
Run Code Online (Sandbox Code Playgroud)
运行时:
$ python code.py
> /home/javl/sandbox/so/debug/code.py(10)<module>()
9
---> 10 g1 = gen()
11
ipdb> n
> /home/javl/sandbox/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.
Run Code Online (Sandbox Code Playgroud)
有调试器命令p和pp这种意愿print,并prettyprint跟随他们任何表情.
所以你可以按如下方式使用它:
$ python code.py
> /home/javl/sandbox/so/debug/code.py(10)<module>()
9
---> 10 g1 = gen()
11
ipdb> n
> /home/javl/sandbox/so/debug/code.py(12)<module>()
11
---> 12 text = "aha" + "bebe"
13
ipdb> p list(g1)
[1, 2, 3, 4, 5]
ipdb> c
Run Code Online (Sandbox Code Playgroud)
还有一个exec命令,通过为表达式添加前缀来调用!,这会强制调试器将表达式作为Python表达式.
ipdb> !list(g1)
[]
Run Code Online (Sandbox Code Playgroud)
欲了解更多详情,请参阅help p,help pp并help 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']
Run Code Online (Sandbox Code Playgroud)