Python learns day 12 debug assertion logging PDB Pdb.set_trace

Source: Internet
Author: User
Tags assert integer division

Debug

The first method is simple and straightforward, which is to print print out the variables that might be problematic:

>>> 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:integ ER division Ormodulo by Zero

  

Assertion

Where print is used to assist in viewing, you can replace it with an assertion (assert):

>>> def foo (s): N=int (s) assertn!=0, ' n is zero ' return10/n>>> def main (): foo (' 0 ') >>> main () Traceback (most recent): File "<pyshell#37>", line 1, in <module>main () file "<pyshell#36>", Li NE 2, in Mainfoo (' 0 ') File "<pyshell#32>", line 3, in Fooassert n!=0, ' n was zero ' assertionerror:n is zero

  

Logging

Replacing print with logging is the 3rd way, and the assert ratio, logging does not throw an error, and can be output to a file:

>>> 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 Way is to launch the Python debugger pdb, to let the program run in a single step, and to view the running state 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 parameter-m PDB, the PDB navigates to the next code to execute, s = ' 0 '. Enter command l to view the code:

(PDB) L

1 # err.py

2-s = ' 0 '

3 n = Int (s)

4 Print 10/n

[EOF]

Enter command N to step through the code:

(PDB) n

>/users/michael/github/sicp/err.py (3) <module> ()

-n = Int (s)

(PDB) n

>/users/michael/github/sicp/err.py (4) <module> ()

Print 10/n

At any time, you can enter the command p variable name to view the variable:

(PDB) P s

' 0 '

(PDB) P n

0

Enter command q to end debugging, exit the program:

(PDB) n

Zerodivisionerror: ' Integer division Ormodulo by Zero '

>/users/michael/github/sicp/err.py (4) <module> ()

Print 10/n

(PDB) Q

Try

This method of debugging through the PDB on the command line is theoretically omnipotent, but it is too cumbersome

Pdb.set_trace ()

This method also uses the PDB, but does not require stepping, we only need to import the PDB, and then, where possible error place a pdb.set_trace (), you can set a breakpoint:

# err.py
Import pdb
s = ' 0 '
n = Int (s)
Pdb.set_trace () # Running to here will automatically pause
print10/n

Running the code, the program will automatically pause in Pdb.set_trace () and go into the PDB debugging environment, you can view the variable with command p, or continue with command C:

$ python err.py

>/users/michael/github/sicp/err.py (7) <module> ()

Print 10/n

(PDB) P n

0

(PDB) C

Traceback (most recent):

File "err.py", line 7, <module>

Print 10/n

Zerodivisionerror:integer Division Ormodulo by Zero

This is much more efficient than a direct-start PDB, but it's also a high-level one.

Python learns day 12 debug assertion logging PDB Pdb.set_trace

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.