【Python进阶(二)】——程序调试方法
🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972
(图片来源网络,侵删)
个人介绍: 研一|统计学|干货分享
擅长Python、Matlab、R等主流编程软件
累计十余项国家级比赛奖项,参与研究经费10w、40w级横向
文章目录
- 1 程序调试基本方法
- 1.1 PDB调试
- 1.2 debug调试
- 2 设置错误信息的显示方式
- 2.1 mode Plain
- 2.2 mode Verbose
- 2.3 debug
- 3 设置断言的方法
【Python进阶(二)】系列——程序调试方法,建议收藏!
该篇文章利用Python演示了程序调试方法,包括程序的基本调试方法里;错误信息显示方式;断言设置方法等内容。
(图片来源网络,侵删)1 程序调试基本方法
1.1 PDB调试
运行程序:
#当python抛出异常或错误信息时,可以用PDB(The Python Debugger,Python调试器)进行程序调试 x=1 x1
运行结果:
Traceback (most recent call last): File "", line 2, in x1 NameError: name 'x1' is not defined
1.2 debug调试
运行程序:
%debug #打开PDB方法,退出PDB方法是输入q或quit #除了PDB,常用的python调试器还有Pylint,Pycheaker
运行结果:
(2)() 1 x=1 ----> 2 x1 ipdb> x 1 ipdb> q
2 设置错误信息的显示方式
2.1 mode Plain
运行程序:
%xmode Plain y=1 Y
运行结果:
(图片来源网络,侵删)Exception reporting mode: Plain Traceback (most recent call last): File "", line 3, in Y NameError: name 'Y' is not defined
2.2 mode Verbose
运行程序:
%xmode Verbose y=1 Y
运行结果:
Exception reporting mode: Verbose --------------------------------------------------------------------------- NameError Traceback (most recent call last) in 1 get_ipython().run_line_magic('xmode', 'Verbose') 2 y=1 ----> 3 Y global Y = undefined NameError: name 'Y' is not defined
2.3 debug
运行程序:
%debug
运行结果:
(3)() 1 get_ipython().run_line_magic('xmode', 'Verbose') 2 y=1 ----> 3 Y ipdb> x 1 ipdb> y 1 ipdb> q
3 设置断言的方法
运行程序:
a=1 b=0 assert b!=0,"分母不能等于0"
运行结果:
AssertionError Traceback (most recent call last) in 1 a=1 2 b=0 ----> 3 assert b!=0,"分母不能等于0" global b = 0 AssertionError: 分母不能等于0
运行程序:
%debug
运行结果:
(3)() 1 a=1 2 b=0 ----> 3 assert b!=0,"分母不能等于0" ipdb> a ipdb> b ipdb> a ipdb> quuit *** NameError: name 'quuit' is not defined ipdb> quit
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...