尝试使用 pdb 调试我的基于 python 的 roguelike。我想弄清楚的是,函数handle_keys()是否像我所问的那样返回0。我有另一个函数没有按预期运行,所以我试图找出问题发生的地方。应该发生的情况是:handle_keys() 返回 0,然后 frag_grenade() 执行某些操作。
我插入了 pdb 的回溯,但我不确定这是否会显示 handle_keys() 是否返回 0:
elif key.vk == libtcod.KEY_BACKSPACE:
game_state = 'playing'
pdb.set_trace()
return 0
当我在游戏中按退格键时,我从 pdb 得到以下输出:
--> return 0
我不确定这是否显示返回值或仅显示下一行代码......
非常感谢!
最佳答案
26.2. pdb — The Python Debugger — Python 2.7.15 documentation :
The typical usage to break into the debugger from a running program is to insert
import pdb; pdb.set_trace()
at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the c command.
箭头指向当前行,即即将执行的行。
返回值的打印方式完全不同:
In [11]: def answer():
...: return 42
In [13]: pdb.runeval("answer()")
> <string>(1)<module>()->None
(Pdb) s
--Call--
> <ipython-input-11-22e067ec9c24>(1)answer()
-> def answer():
(Pdb) n
> <ipython-input-11-22e067ec9c24>(2)answer()
-> return 42
(Pdb)
--Return--
> <ipython-input-11-22e067ec9c24>(2)answer()->42
-> return 42
(Pdb)
正如您所看到的,返回行被打印两次——第一次是在执行之前,然后是函数返回时。第二次,它在位置行中伴随有 --Return--
和返回值。
关于python - 了解 pdb.set_trace() 之后的 pdb 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862946/