Python3: 调式Debugging

Debugging

# 利用好调试,能大大提高捕捉代码Bug,大部分人总是忽略Python debugger(pdb)的重要性
import pdb
def make_bread():
    pdb.set_trace()
    return "I don't have time."

print(make_bread())
> <ipython-input-4-c3676a6348d9>(4)make_bread()
-> return "I don't have time."
(Pdb) w
  d:\anaconda3\lib\runpy.py(193)_run_module_as_main()
-> "__main__", mod_spec)
  d:\anaconda3\lib\runpy.py(85)_run_code()
-> exec(code, run_globals)
  d:\anaconda3\lib\site-packages\ipykernel_launcher.py(16)<module>()
-> app.launch_new_instance()
  d:\anaconda3\lib\site-packages\traitlets\config\application.py(658)launch_instance()
-> app.start()
  d:\anaconda3\lib\site-packages\ipykernel\kernelapp.py(486)start()
-> self.io_loop.start()
  d:\anaconda3\lib\site-packages\tornado\platform\asyncio.py(127)start()
-> self.asyncio_loop.run_forever()
  d:\anaconda3\lib\asyncio\base_events.py(422)run_forever()
-> self._run_once()
  d:\anaconda3\lib\asyncio\base_events.py(1432)_run_once()
-> handle._run()
  d:\anaconda3\lib\asyncio\events.py(145)_run()
-> self._callback(*self._args)
  d:\anaconda3\lib\site-packages\tornado\platform\asyncio.py(117)_handle_events()
-> handler_func(fileobj, events)
  d:\anaconda3\lib\site-packages\tornado\stack_context.py(276)null_wrapper()
-> return fn(*args, **kwargs)
  d:\anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py(450)_handle_events()
-> self._handle_recv()
  d:\anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py(480)_handle_recv()
-> self._run_callback(callback, msg)
  d:\anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py(432)_run_callback()
-> callback(*args, **kwargs)
  d:\anaconda3\lib\site-packages\tornado\stack_context.py(276)null_wrapper()
-> return fn(*args, **kwargs)
  d:\anaconda3\lib\site-packages\ipykernel\kernelbase.py(283)dispatcher()
-> return self.dispatch_shell(stream, msg)
  d:\anaconda3\lib\site-packages\ipykernel\kernelbase.py(233)dispatch_shell()
-> handler(stream, idents, msg)
  d:\anaconda3\lib\site-packages\ipykernel\kernelbase.py(399)execute_request()
-> user_expressions, allow_stdin)
  d:\anaconda3\lib\site-packages\ipykernel\ipkernel.py(208)do_execute()
-> res = shell.run_cell(code, store_history=store_history, silent=silent)
  d:\anaconda3\lib\site-packages\ipykernel\zmqshell.py(537)run_cell()
-> return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  d:\anaconda3\lib\site-packages\ipython\core\interactiveshell.py(2662)run_cell()
-> raw_cell, store_history, silent, shell_futures)
  d:\anaconda3\lib\site-packages\ipython\core\interactiveshell.py(2785)_run_cell()
-> interactivity=interactivity, compiler=compiler, result=result)
  d:\anaconda3\lib\site-packages\ipython\core\interactiveshell.py(2909)run_ast_nodes()
-> if self.run_code(code, result):
  d:\anaconda3\lib\site-packages\ipython\core\interactiveshell.py(2963)run_code()
-> exec(code_obj, self.user_global_ns, self.user_ns)
  <ipython-input-4-c3676a6348d9>(6)<module>()
-> print(make_bread())
> <ipython-input-4-c3676a6348d9>(4)make_bread()
-> return "I don't have time."
(Pdb) s
--Return--
> <ipython-input-4-c3676a6348d9>(4)make_bread()->"I don't have time."
-> return "I don't have time."
(Pdb) n
--Call--
> d:\anaconda3\lib\site-packages\ipykernel\iostream.py(366)write()
-> def write(self, string):
(Pdb) c
I don't have time.
# 上个kernel运行后马上进入debugger模式
# 命令列表:
# c : 继续执行
# w : 显示当前正在执行的代码行的上下文信息
# a :打印当前函数的参数列表
# s : 执行当前代码行,并停在第一个能停的地方(相当于单步进入)
# n : 继续执行到当前函数的下一行,或者当前行直接返回(单步跳过)
# 更多命令查看官方文档

先占个坑,后续调式技巧再补……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZPILOTE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值