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:
- #!/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)
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 ).
- #!/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. Start running Python program debugging
Run the program./debug_demo.py.
- > /usr/local/qspace/user_network/debug_demo.py(7)debug_demo()
- -> if val <= 1600 :
- (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)
- (PDB)
- _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
- (PDB)
The row number on the left and the code body on the right.
Monitoring variable: p variable name
- (PDB) p val
- 4500
- (PDB)
One-step execution: n
- -> elif val <= 3500 :
- (PDB)
- 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
Add breakpoint: B row number
- (PDB) b 14
Run to breakpoint: c
- (PDB) c
- > /*****
- -> print "level 3"
- (PDB)
- print 0
- elif val <= 3500 :
- print "level 2"
- print (val - 1600) * 0.05
- elif val <= 6500 :
- B-> 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
Before the function is returned: r
- (PDB) r
- level 3
- 195.0
- --Return--
- > /****()
- ->None
- -> print (val - 3500) * 0.10 + (3500-1600) * 0.05
- (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.