python快捷调试方法

欢迎访问Coolver教程

你好! 这是你第一次使用Coolver的教程。如果你想学习更多相关知识和第一手资料, 可以加入我创建的QQ群:818045141,可以跟更多朋友及我本人交流。

简单介绍

调试应用程序有时可能是不受欢迎的活动。您正忙于时间紧迫的工作,只是希望它能工作。但是,在其他时候,您可能正在学习一种新的语言功能或尝试一种新的方法,并且想更深入地了解某些东西是如何工作的。

无论哪种情况,调试代码都是必需的,因此最好在调试器中工作是一个好主意。在本教程中,我将向您展示使用pdb(Python的交互式源代码调试器)的基础。

我将向您介绍pdb的一些常见用法。您可能需要将本教程添加为书签,以便以后在实际需要时可以快速参考。pdb和其他调试器是必不可少的工具。当您需要调试器时,没有替代品。您真的需要它。

在本教程结束时,您将知道如何使用调试器查看应用程序中任何变量的状态。您还可以随时停止和恢复应用程序的执行流程,因此您可以准确地看到每一行代码如何影响其内部状态。

这对于跟踪难以发现的错误非常有用,并且可以使您更快,更可靠地修复错误代码。有时,单步执行pdb中的代码并查看值如何更改可能会真正使人大开眼界,并导致“ aha”时刻以及偶尔的“脸掌”。

pdb是Python标准库的一部分,因此它始终存在并且可以使用。如果您需要在无法访问自己熟悉的GUI调试器的环境中调试代码,则可以节省生命。

快捷的使用方式

你可以在需要的地方插入此行代码,然后正常运行程序即可:
import pdb; pdb.set_trace()

python3.7及以上的优化

python3.7开始内置了调试的方法,您只需要简单地加一句:
breakpoint()
代码就是自动的调用import pdb; pdb.set_trace()来中断

如何使用

CommandDescription
pPrint the value of an expression.
ppPretty-print the value of an expression.
nContinue execution until the next line in the current function is reached or it returns.
sExecute the current line and stop at the first possible occasion (either in a function that is called or in the current function).
cContinue execution and only stop when a breakpoint is encountered.
untContinue execution until the line with a number greater than the current one is reached. With a line number argument, continue execution until a line with a number greater or equal to that is reached.
lList source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing.
llList the whole source code for the current function or frame.
bWith no arguments, list all breaks. With a line number argument, set a breakpoint at this line in the current file.
wPrint a stack trace, with the most recent frame at the bottom. An arrow indicates the current frame, which determines the context of most commands.
uMove the current frame count (default one) levels up in the stack trace (to an older frame).
dMove the current frame count (default one) levels down in the stack trace (to a newer frame).
hSee a list of available commands.
h Show help for a command or topic.
hpdb Show the full pdb documentation.
qQuit the debugger and exit.

reference

参考列表如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值