Use PDB for Python program debugging

Source: Internet
Author: User

For most programmers, mastering more than two programming language application methods is their necessary professional skills. In this case, Python is one of the many languages. Here we can understand the application characteristics of this language through the interpretation of related methods for Python program debugging.

Python program debugging source code example

For example, there is a program to simulate tax calculation:

 
 
  1. #!/usr/bin/python   
  2. def debug_demo(val):   
  3. if val <= 1600 :   
  4. print "level 1"   
  5. print 0   
  6. elif val <= 3500 :   
  7. print "level 2"   
  8. print (val - 1600) * 0.05   
  9. elif val <= 6500 :   
  10. print "level 3"   
  11. print (val - 3500) * 0.10 + (3500-1600) * 0.05   
  12. else:   
  13. print "level 4"   
  14. print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05   
  15. #~def debug_demo   
  16. if __name__ == "__main__":   
  17. debug_demo(4500) 

The debug_demo function calculates the tax required for 4500 of the account.

How to debug Python programs?

1. Add a breakpoint

Add the red part of the code where the breakpoint needs to be inserted: if the value of _ DEBUG is True, add _ DEBUG to enable or disable debugging ).

 
 
  1. #!/usr/bin/python   
  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   
  13. elif val <= 6500 :   
  14. 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. #~def debug_demo   
  20. if __name__ == "__main__":   
  21. debug_demo(4500)  

2. Start running Python program debugging

Run the program./debug_demo.py.

 
 
  1. > /usr/local/qspace/user_network/debug_demo.py(7)debug_demo()   
  2. -> if val <= 1600 :   
  3. (PDB) 

-> Val <= 1600: indicates the statement currently executed, PDB) Wait for your debugging command. PDB has a wide range of commands. Enter the h command to view how to use the command. The following describes common commands for Python program debugging:

View the code context, l lower case L)

 
 
  1. (PDB)   
  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   
  13. (PDB)  

The row number on the left and the code body on the right.

Monitoring variable: p variable name

 
 
  1. (PDB) p val   
  2. 4500   
  3. (PDB) 

One-step execution: n

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

Add breakpoint: B row number

 
 
  1. (PDB) b 14 

Run to breakpoint: c

 
 
  1. (PDB) c   
  2. > /*****   
  3. -> print "level 3"   
  4. (PDB)  
  5. print 0   
  6. elif val <= 3500 :   
  7. print "level 2"   
  8. print (val - 1600) * 0.05   
  9. elif val <= 6500 :   
  10. B-> print "level 3"   
  11. print (val - 3500) * 0.10 + (3500-1600) * 0.05   
  12. else:   
  13. print "level 4"   
  14. print (val - 6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05  

Before the function is returned: r

 
 
  1. (PDB) r   
  2. level 3   
  3. 195.0   
  4. --Return--   
  5. > /****()   
  6. ->None   
  7. -> print (val - 3500) * 0.10 + (3500-1600) * 0.05   
  8. (PDB) 

Note:

PDB also has many other useful commands that readers can explore on their own. Enter the h and h commands. You can get the Detailed Help of the command.

However, I personally think that you do not need to start this Python program debugging method. Generally, you can use log output for debugging unless you encounter a very subtle error. Then, the power of one-step debugging is displayed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.