清單 7. 使用函數(shù)的例子
import pdb
def combine(s1,s2): # define subroutine combine, which.。。
s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
return s3 # and returns it.
a = “aaa”
pdb.set_trace()
b = “bbb”
c = “ccc”
final = combine(a,b)
print final
如果直接使用 n 進(jìn)行 debug 則到 final=combine(a,b) 這句的時(shí)候會(huì)將其當(dāng)做普通的賦值語(yǔ)句處理,進(jìn)入到 print final。如果想要對(duì)函數(shù)進(jìn)行 debug 如何處理呢 ? 可以直接使用 s 進(jìn)入函數(shù)塊。函數(shù)里面的單步調(diào)試與上面的介紹類(lèi)似。如果不想在函數(shù)里單步調(diào)試可以在斷點(diǎn)處直接按 r 退出到調(diào)用的地方。
清單 8. 對(duì)函數(shù)進(jìn)行 debug
[root@rcc-pok-idg-2255 ~]# python epdb2.py
》 /root/epdb2.py(10)?()
-》 b = “bbb”
(Pdb) n
》 /root/epdb2.py(11)?()
-》 c = “ccc”
(Pdb) n
》 /root/epdb2.py(12)?()
-》 final = combine(a,b)
(Pdb) s
--Call--
》 /root/epdb2.py(3)combine()
-》 def combine(s1,s2): # define subroutine combine, which.。。
(Pdb) n
》 /root/epdb2.py(4)combine()
-》 s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
(Pdb) list
import pdb
def combine(s1,s2): # define subroutine combine, which.。。
-》 s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
return s3 # and returns it.
a = “aaa”
pdb.set_trace()
b = “bbb”
c = “ccc”
(Pdb) n
》 /root/epdb2.py(5)combine()
-》 s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
(Pdb) n
》 /root/epdb2.py(6)combine()
-》 return s3 # and returns it.
(Pdb) n
--Return--
》 /root/epdb2.py(6)combine()-》‘“aaabbbaaa”’
-》 return s3 # and returns it.
(Pdb) n
》 /root/epdb2.py(13)?()
-》 print final
(Pdb)
在調(diào)試的時(shí)候動(dòng)態(tài)改變值 。在調(diào)試的時(shí)候可以動(dòng)態(tài)改變變量的值,具體如下實(shí)例。需要注意的是下面有個(gè)錯(cuò)誤,原因是 b 已經(jīng)被賦值了,如果想重新改變 b 的賦值,則應(yīng)該使用! B。
清單 9. 在調(diào)試的時(shí)候動(dòng)態(tài)改變值
[root@rcc-pok-idg-2255 ~]# python epdb2.py
》 /root/epdb2.py(10)?()
-》 b = “bbb”
(Pdb) var = “1234”
(Pdb) b = “avfe”
*** The specified object ‘= “avfe”’ is not a function
or was not found along sys.path.
(Pdb) !b=“afdfd”
(Pdb)
pdb 調(diào)試有個(gè)明顯的缺陷就是對(duì)于多線程,遠(yuǎn)程調(diào)試等支持得不夠好,同時(shí)沒(méi)有較為直觀的界面顯示,不太適合大型的 python 項(xiàng)目。而在較大的 python 項(xiàng)目中,這些調(diào)試需求比較常見(jiàn),因此需要使用更為高級(jí)的調(diào)試工具。
評(píng)論