Python提供類似gdb的除錯模組-pdb
使用方法如下
python -m pdb 檔案名稱.py
進入pdb後
常用命令如下
b 數字 - 設置中斷點
r - 繼續執行,直到當前函式返回
c - 繼續執行程式
n - 執行下一行程式
s - 進入函式
p 變數名稱 - 印出變數
l - 列出目前的程式片段
q - 離開
例如
#def.py
def max(*a):
num=0
for n in a:
if(n>num):
num=n
return num
print(max(10,20,11,21,50,40,100,30))
C:\Users\CecilYang\Desktop>python -m pdb def.py
> c:\users\cecilyang\desktop\def.py(2)<module>()
-> def max(*a):
(Pdb) b 9
Breakpoint 1 at c:\users\cecilyang\desktop\def.py:9
(Pdb) c
> c:\users\cecilyang\desktop\def.py(9)<module>()
-> print(max(10,20,11,21,50,40,100,30))
(Pdb) s
--Call--
> c:\users\cecilyang\desktop\def.py(2)max()
-> def max(*a):
(Pdb) n
> c:\users\cecilyang\desktop\def.py(3)max()
-> num=0
(Pdb) n
> c:\users\cecilyang\desktop\def.py(4)max()
-> for n in a:
(Pdb) l
1 #def.py
2 def max(*a):
3 num=0
4 -> for n in a:
5 if(n>num):
6 num=n
7 return num
8
9 B print(max(10,20,11,21,50,40,100,30))
[EOF]
(Pdb) n
> c:\users\cecilyang\desktop\def.py(5)max()
-> if(n>num):
(Pdb) p n
10
(Pdb) r
--Return--
> c:\users\cecilyang\desktop\def.py(7)max()->100
-> return num
(Pdb) n
100
--Return--
> c:\users\cecilyang\desktop\def.py(9)<module>()->None
-> print(max(10,20,11,21,50,40,100,30))
(Pdb) n
--Return--
> <string>(1)<module>()->None
(Pdb) n
The program finished and will be restarted
> c:\users\cecilyang\desktop\def.py(2)<module>()
-> def max(*a):
(Pdb) q