只需在发电机上拨打help p即可。
lst = list(gen)
lst
请注意,这会影响不会返回任何其他项目的生成器。
您也无法在IPython中直接调用help p,因为它与列出代码行的命令冲突。
测试此文件:
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/sandbox/so/debug/code.py(10)()
9
---> 10 g1 = gen()
11
ipdb> n
> /home/javl/sandbox/so/debug/code.py(12)()
11
---> 12 text = "aha" + "bebe"
13
ipdb> lst = list(g1)
ipdb> lst
[1, 2, 3, 4, 5]
ipdb> q
Exiting Debugger.
转义函数/变量/调试器名称冲突的一般方法
存在调试器命令help p和help pp,其将在help exec和prettyprint之后的任何表达式。
所以你可以按如下方式使用它:
$ python code.py
> /home/javl/sandbox/so/debug/code.py(10)()
9
---> 10 g1 = gen()
11
ipdb> n
> /home/javl/sandbox/so/debug/code.py(12)()
11
---> 12 text = "aha" + "bebe"
13
ipdb> p list(g1)
[1, 2, 3, 4, 5]
ipdb> c
还有一个help p命令,通过在表达式前加上help pp来强制调用,它强制调试器将表达式作为Python表达式。
ipdb> !list(g1)
[]
有关详细信息,请参阅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']