It's a bald head! Python also has PDB

Little Zhan 2020-11-17 17:02:31
bald head python pdb


writing | Xuanyuan Yulong

source :Python technology 「ID: pythonall」

C Yes gdb,Python Also have pdb

Yes C Language students must miss ( readers :¿)gdb The debugger , Use gdb You can pause the process freely while the program is running 、 Check the variable .

A lot of times , We simply analyze the code flow and log information can not locate the problem , It all depends on the debugger ; It can be said that with the debugger , The programmer is the God of the code world .

Python Of course, it doesn't show weakness , The same tower of Babel can make people Ascend to heaven

01

—— But what sauce has to admit is , modern IDE The integrated graphical debugging function is very good , In general, there are not many scenarios using command-line tools .

But there is also the inability to use graphics IDE The situation of , So right. pdb It is necessary to know a little about the tools . After all, no one knows what kind of environment it may be thrown into. Ha ha

pdb Use

As an explanatory language ,Python The use of debugging tools and gdb After all, there is a difference .

such as Python You don't need a symbol table to debug , At the end of the day , Final Python The logic executed by the virtual machine is also self signed .

Because Python This particularity of , all pdb In fact, there are two different ways to use it , They are invasive and non-invasive .

In fact, the literal meaning is easy to understand in two ways of using . Let's take a brain computer interface , It can also be divided into invasive and non-invasive . Intrusive means to put the electrode 、 The chip is implanted in the cerebral cortex ,“ invasion ” human body ; The noninvasive method is to collect brain waves outside the skull for analysis .

similarly , Intrusive pdb Call is to call pdb The code is written directly into Python In the script ; Non intrusive calls from the command line pdb, Execute the corresponding debugged script .

Intrusive pdb

How to use it is shown in the following code , Insert a line in the middle of the code to call :

import pdb; # pdb.set_trace()
a = "just"
b = "do"
pdb.set_trace()
c = ['p', 'y', 't', 'h', 'o', 'n']
print(a)

Run script , It's going to be an interactive interface :

D:\000-GitHub\python-examples\xuanyuanyulong\2020-11-04-python-pdb>python test_pdb_intrusive.py
> d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py(21)<module>()
-> c = ['p', 'y', 't', 'h', 'o', 'n']
(Pdb)

It's been activated here pdb, And in the printed content -> c = ['p', 'y', 't', 'h', 'o', 'n'] The arrow at the beginning of the line , Indicates that the current program execution flow to this line of code , If I continue , The line will be executed first .

Non intrusive pdb

Noninvasive should be xue A little bit simpler , The biggest benefit is that you don't need to change the code .

We execute the following commands from the console :

D:\000-GitHub\python-examples\xuanyuanyulong\2020-11-04-python-pdb>python -m pdb test_pdb_intrusive.py
> d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py(1)<module>()
-> import pdb; # pdb.set_trace()
(Pdb)

You can see , Enter debugging in this way , The program execution flow stops at the beginning of the program .

By analyzing the location of the code execution flow when entering debugging , We can find out , In fact, intrusive insertion pdb.set_trace() call , It's equivalent to starting from the command line pdb, Then a breakpoint is made on the next line of the call , And then run the program directly .

A simple command

gdb There are some simple commands that are commonly used in , Let's get familiar with this section , More in-depth discussions will be held later .

h(elp)

stay pdb Input under interface h or help command , You can list pdb Various commands supported in :

(Pdb) h
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================
exec pdb

stay pdb With a command as an argument , You can also see the corresponding instructions for use :

(Pdb) h h
h(elp)
Without argument, print the list of available commands.
With a command name as argument, print help about that command.
"help pdb" shows the full pdb documentation.
"help exec" gives help on the ! command.

believe me ,help It's actually pdb The most important command in it . You can't remember anything else , however help Remember that . The same is true in a result oriented workplace life , When you encounter problems, you should ask for help in time ~

l(ist)

Print the source code of the current file . Without parameters , By default, print the total amount before and after the current line 11 Line code . If you continue with the order , It will continue to print the most 11 Line code , Until the end of the file EOF.

use . As a parameter, the limit should be stronger , Only before and after the current line will be printed 11 Line code .

(Pdb) l
1 -> import pdb; # pdb.set_trace()
2
3
4 def addStr(a, b):
5 return a + b
6
8 return ''.join(l)
9
10 def getSlogan(a, b, c):
11 result = addStr(a, b) + mergeChar(c)

When you specify two parameters , Print the code in this interval :

(Pdb) l 3, 7
3
4 def addStr(a, b):
5 return a + b
6
7 -> def mergeChar(l: list):

And when the second parameter b Than the first parameter a When I was a child , said “ From a OK, let's start , continue Print back b That's ok ”, That is to say, total printing (1+b) That's ok :

(Pdb) l 7, 3
7 -> def mergeChar(l: list):
8 return ''.join(l)
9
10 def getSlogan(a, b, c):

p/pp

Print the value of an object . The difference lies in pp It's called pprint function , Printing is more beautiful .

(Pdb) p a
'just'
(Pdb) p addStr
<function addStr at 0x000002087B0F9C80>

!

Use ! Can be in pdb In the environment , Carry out general Python sentence . Usually we can change the value of a variable —— How else can a debugger make you God ? Is there anything more arbitrary than that ?

Generally speaking, this ! In fact, it can be omitted , But when you want to execute the word at the beginning of a statement with pdb The existing command conflict of , You don't get the results you expect , So it is suggested to add .

This one uses ! The way to lead an order is to vim The logic of the editor is very similar to , It can be compared to memory . Unfamiliar readers can ignore .

(Pdb) !a = "python"
(Pdb) p a
'python'

r(eturn)

pdb in ,r and return It means the same thing , namely “ Run the current function until it returns ”.

This point ,r stay pdb and gdb The meaning is different . Readers don't have to care about

run/restart

Means to rerun the currently debugged program . Use this command , You can pass in the required parameters for scripts that need to pass in parameters .

The format is the same as the command line to execute the script , Just put the corresponding python Command and script paths are replaced with run or restart.

(Pdb) run a b c d kkk
Restarting test_pdb_intrusive.py with arguments:
test_pdb_intrusive.py
> d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py(1)<module>()
-> import pdb; # pdb.set_trace()
(Pdb) !import sys
(Pdb) p sys.argv
['test_pdb_intrusive.py', 'a', 'b', 'c', 'd', 'kkk']

b(reak)

see / Add Breakpoint .

Without any parameters , The current breakpoints are listed .

(Pdb) b 21
Breakpoint 1 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
(Pdb) b 17
Breakpoint 2 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
2 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17

meanwhile , adopt run and restart Rerun the debugged program , Existing breakpoints will not be cleared :

(Pdb) run
Restarting test_pdb_intrusive.py with arguments:
test_pdb_intrusive.py
> d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py(1)<module>()
-> import pdb; # pdb.set_trace()
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
2 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17

cl(ear)

There are three ways to use it :1) Similar to setting breakpoints , Clear breakpoints on specific lines of specific files ;2) List the breakpoints to be cleared , Space off ;3) With no arguments , Clear all breakpoints .

Let's show you one by one :

1) Similar to setting breakpoints , Clear breakpoints on specific lines of specific files

(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
2 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
(Pdb) clear test_pdb_intrusive.py:21
(Pdb) b
Num Type Disp Enb Where
2 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17

2) List the breakpoints to be cleared , Space off

(Pdb) b 21
Breakpoint 3 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
(Pdb) b 15
Breakpoint 4 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) b
Num Type Disp Enb Where
2 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
3 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
4 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) clear 2 4
Deleted breakpoint 2 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
Deleted breakpoint 4 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) b
Num Type Disp Enb Where
3 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21

3) With no arguments , Clear all breakpoints

(Pdb) b 17
Breakpoint 5 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
(Pdb) b 15
Breakpoint 6 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) b
Num Type Disp Enb Where
3 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
5 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
6 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) b
Num Type Disp Enb Where
3 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
5 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
6 breakpoint keep yes at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) clear
Clear all breaks? yes
Deleted breakpoint 3 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:21
Deleted breakpoint 5 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:17
Deleted breakpoint 6 at d:\000-github\python-examples\xuanyuanyulong\2020-11-04-python-pdb\test_pdb_intrusive.py:15
(Pdb) b

All right, stop, stop , Feeling before writing pdb Not much , I didn't expect to find out until I wrote it , That's a little bit more , I stayed up late again today ……

02

Dog life matters I'll see you later 、 See you around

summary

pdb The content is surprisingly rich , There are a lot of things that are not covered in this article .

Software debugging is actually a very interesting knowledge , Of course , It's also a very useful knowledge

Due to the revision of wechat platform algorithm , The public name content will no longer be displayed in chronological order , If you want to see our push for the first time , We are strongly advised to star mark us and give us more 【 Looking at 】. The specific steps are as follows :

(1) Click at the top of the page “ Xiaozhan school Python”, Access to the official account homepage .
(2) Click on the little dot in the upper right corner , Click... On the pop-up page “ Set to star ”, That's all right. .
Thank you for your support , finger heart .

This article is from WeChat official account. - Xiaozhan school Python(xiaozhan_Python)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-11-13

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Little Zhan]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database