Python learning Day 12 debugging assertions logging pdb. set_trace, pdbpdb. set_trace
Debugging
The first method is simple, straightforward, and effective.printPrint out the variables that may have problems:
>>> def foo(s):n= int(s)print '>>> n = %d' % nreturn 10 / n>>> def main():foo('0')>>> main()>>> n = 0Traceback (most recent call last):File "<pyshell#25>", line 1, in <module>main()File "<pyshell#24>", line 2, in mainfoo('0')File "<pyshell#22>", line 4, in fooreturn 10 / nZeroDivisionError: integer division ormodulo by zero
Assertions
Where print is used for viewing, assert can be used instead:
>>> def foo(s):n=int(s)assertn!=0,'n is zero'return10/n>>> def main():foo('0')>>> main()Traceback (most recent call last):File "<pyshell#37>", line 1, in <module>main()File "<pyshell#36>", line 2, in mainfoo('0')File "<pyshell#32>", line 3, in fooassert n!=0,'n is zero'AssertionError: n is zero
Logging
Replace print with logging in the following 3rd methods. Compared with assert, logging does not throw errors and can be output to files:
>>> import logging>>>logging.basicConfig(level=logging.INFO)>>> s='0'>>> n=int(s)>>> logging.info('n=%d' % n)>>> print 10/n Traceback (most recent call last):File "<pyshell#48>", line 1, in <module>print 10/nZeroDivisionError: integer division ormodulo by zero
Pdb
The 4th method is to start the Python debugger pdb so that the program runs in one step and the running status can be viewed at any time.
# err.pys = '0'n = int(s)print 10 / n$ python -m pdb err.py> /Users/michael/Github/sicp/err.py(2)<module>()-> s = '0'
After starting with the-m pdb parameter, pdb locates the code to be executed next-> s = '0 '. Enter the command l to view the Code:
(Pdb) l
1 # err. py
2-> s = '0'
3 n = int (s)
4 print 10/n
[EOF]
Enter the command n to execute the code in one step:
(Pdb) n
>/Users/michael/Github/SiC/err. py (3) <module> ()
-> N = int (s)
(Pdb) n
>/Users/michael/Github/SiC/err. py (4) <module> ()
-> Print 10/n
You can enter the p variable name to view the variable at any time:
(Pdb) p s
'0'
(Pdb) p n
0
Enter the q command to end debugging and exit the program:
(Pdb) n
ZeroDivisionError: 'integer division ormodulo by 0'
>/Users/michael/Github/SiC/err. py (4) <module> ()
-> Print 10/n
(Pdb) q
Try
This method of debugging through pdb is theoretically omnipotent, but it is too troublesome.
Pdb. set_trace ()
This method also uses pdb, but does not need to be executed in a single step. We only need to import pdb, and then put a pdb. set_trace () in the place where an error may occur to set a breakpoint:
# err.py
import pdb
s = '0'
n = int(s)
Pdb. set_trace () # It is automatically paused when it runs here.
print10 / n
Run the code. The program will automatically pause in pdb. set_trace () and enter the pdb debugging environment. You can run the command p to view the variable, or run the command c:
$ Python err. py
>/Users/michael/Github/SiC/err. py (7) <module> ()
-> Print 10/n
(Pdb) p n
0
(Pdb) c
Traceback (most recent call last ):
File "err. py", line 7, in <module>
Print 10/n
ZeroDivisionError: integer division ormodulo by zero
This method is much more efficient than directly starting pdb for single-step debugging, but it is also less efficient.