Python程式調試)

來源:互聯網
上載者:User

本文討論在沒有方便的IDE工具可用的情況下,使用pdb調試python程式

源碼例子

例如,有類比稅收計算的程式:

#!/usr/bin/python
def debug_demo(val):
                if val <= 1600 :
                                print "level 1"
                                print 0
                elif val <= 3500 :
                                print "level 2"
                                print (val - 1600) * 0.05
                elif val <= 6500 :
                                print "level 3"
                                print (val - 3500) * 0.10 + (3500-1600) * 0.05
                else:
                                print "level 4"
                                print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
#~def debug_demo                
if __name__ == "__main__":
                debug_demo(4500)

debug_demoFunction Compute4500的入賬所需的稅收。

如何調試?

1.加入斷點

在需要插入斷點的地方,加入紅色部分代碼:如果_DEBUG值為True,則在該處開始調試(加入_DEBUG的原因是為了方便開啟/關閉調試)。

#!/usr/bin/python
_DEBUG=True
def debug_demo(val):
                if _DEBUG == True:
                                import pdb
                                pdb.set_trace()
                if val <= 1600 :
                                print "level 1"
                                print 0
                elif val <= 3500 :
                                print "level 2"
                                print (val - 1600) * 0.05
                elif val <= 6500 :
                                print "level 3"
                                print (val - 3500) * 0.10 + (3500-1600) * 0.05
                else:
                                print "level 4"
                                print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
#~def debug_demo                
if __name__ == "__main__":
                debug_demo(4500)

2.開始運行調試

  運行程式./debug_demo.py,得到

> /usr/local/qspace/user_network/debug_demo.py(7)debug_demo()
-> if val <= 1600 :
(Pdb)

-> val <= 1600 : 指示當前執行的語句,(Pdb)等待你的調試指令. pdb的指令很豐富,輸入h指令可以查看指令的使用方法。下面簡單介紹常用指令:

  查看代碼上下文,l(小寫L)

(Pdb) l
    2         _DEBUG=True
    3         def debug_demo(val):
    4                         if _DEBUG == True:
    5                                         import pdb
    6                                         pdb.set_trace()
    7    ->                 if val <= 1600 :
    8                                         print "level 1"
    9                                         print 0
  10                         elif val <= 3500 :
  11                                         print "level 2"
  12                                         print (val - 1600) * 0.05
(Pdb)    

左邊是行號,右邊是代碼本文。

  監視變數:p 變數名

(Pdb) p val
4500
(Pdb)    

逐步執行: n

-> elif val <= 3500 :
(Pdb) l
  5                                         import pdb
  6                                         pdb.set_trace()
  7                         if val <= 1600 :
  8                                         print "level 1"
  9                                         print 0
10    ->                 elif val <= 3500 :
11                                         print "level 2"
12                                         print (val - 1600) * 0.05
13                         elif val <= 6500 :
14                                         print "level 3"
15                                         print (val - 3500) * 0.10 + (3500-1600) * 0.05

加入斷點:b 行號

(Pdb) b 14

運行到斷點: c

(Pdb) c
> /*****
-> print "level 3"
(Pdb) l
  9                                         print 0
10                         elif val <= 3500 :
11                                         print "level 2"
12                                         print (val - 1600) * 0.05
13                         elif val <= 6500 :
14 B->                                 print "level 3"
15                                         print (val - 3500) * 0.10 + (3500-1600) * 0.05
16                         else:
17                                         print "level 4"
18                                         print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
19    

執行到函數返回前: r

(Pdb) r
level 3
195.0
--Return--
> /****()

->None
-> print (val - 3500) * 0.10 + (3500-1600) * 0.05
(Pdb)    

說明:

  pdb還有很多其他很多有用的指令,讀者可以自行探索。輸入h,h 命令。就可以得到命令的詳細協助。

  不過,我個人認為一般無需啟動這種調試方法,一般使用日誌輸出進行調試即可,除非遇到了非常微妙的錯誤。這時,單步調試的威力便顯示出來了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.