pdb的display
命令似乎只在分配整个变量时显示一个表达式。
pass
foo = [1, 2, 3]
foo[0] = 8
foo.append(7)
pass
使用python3 -m pdb bar.py
运行上述代码然后输入display foo
时获得的输出是:
> bar.py(2)<module>()
-> foo = [1, 2, 3]
(Pdb) display foo
display foo: ** raised NameError: name 'foo' is not defined **
(Pdb) n
> bar.py(3)<module>()
-> foo[0] = 8
display foo: [1, 2, 3] [old: ** raised NameError: name 'foo' is not defined **]
(Pdb)
> bar.py(4)<module>()
-> foo.append(7)
(Pdb)
> bar.py(5)<module>()
-> pass
(Pdb) foo
[8, 2, 3, 7]
(Pdb)
需要NameError和显示的第一个输出。
但是,当第一个项目被更改或者我们追加7时,显示器似乎没有发现foo
已更改。
有没有办法告诉pdb列出这些更改?
似乎display str(foo)
完成了这项工作,但我想知道是否有更好的解决方案。
答案 0 :(得分:0)
是的,跟踪这些更改的方法是改为'p'
命令。见这个例子
> /home/maksim/Projects/Python/basic/test.py(2)<module>()
-> foo = [1, 2, 3]
(Pdb) s;;l;;p foo
> /home/maksim/Projects/Python/basic/test.py(3)<module>()
-> foo[0] = 8
1 pass
2 foo = [1, 2, 3]
3 -> foo[0] = 8
4 foo.append(7)
5 pass
6
7
8
[EOF]
[1, 2, 3]
(Pdb) s;;l;;p foo
> /home/maksim/Projects/Python/basic/test.py(4)<module>()
-> foo.append(7)
1 pass
2 foo = [1, 2, 3]
3 foo[0] = 8
4 -> foo.append(7)
5 pass
6
7
8
[EOF]
[8, 2, 3]
(Pdb) s;;l;;p foo
> /home/maksim/Projects/Python/basic/test.py(5)<module>()
-> pass
1 pass
2 foo = [1, 2, 3]
3 foo[0] = 8
4 foo.append(7)
5 -> pass
6
7
8
[EOF]
[8, 2, 3, 7]
(Pdb) s;;l;;p foo
--Return--
> /home/maksim/Projects/Python/basic/test.py(5)<module>()->None
-> pass
1 pass
2 foo = [1, 2, 3]
3 foo[0] = 8
4 foo.append(7)
5 -> pass
6
7
8
[EOF]
[8, 2, 3, 7]
(Pdb)