Python Learning note __8.2 debugging

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1. Overview

How to know which variables are correct and which values are wrong when the program goes wrong.

1.1. Print ()

Print () The variables that may be problematic

def foo (s):

n = Int (s)

print (' >>> n =%d '% n)

Return 10/n

1.2. Assertion

where print () is used to assist in viewing, an assertion (assert) can be used instead:

def foo (s):

n = Int (s)

assert n! = 0, ' n is zero! ' # n!=0 To continue to execute backwards. n=0, it will output 'n is zero'

Return 10/n

If the assertion fails, the Assert statement itself throws Assertionerror.

To perform the program's aesthetics, you can turn off the assert with the-o parameter when you start the Python interpreter

$ python-o err.py

1.3 , 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

# Specifies the level of logging information, with several levels such as Debug,info,warning,error.

# The level that follows will invalidate the previous level. If info causes Debug to fail

logging.basicconfig (level=logging.info)  

s = ' 0 '

n = Int (s)

Logging.info (' n =%d '% n)

Print (10/n)

1.4. pdb

Launch the Python debugger pdb, let the program run in a single step, can view the running state at any time

$ python-m pdb err.py

>/users/michael/github/learn-python3/samples/debug/err.py (2) <module> ()

s = ' 0 ' # automatically prints the first line

(PDB) # changes to debug mode waiting for input instruction

Common directives are:

1: View Code

N: Single Step execution

P: Connect variable name, view variable

Q: Exit

1.5, Pdb.set_trace ()

This method also uses the PDB, but does not require stepping. We need to import the PDB, and then we can set a breakpoint by placing a pdb.set_trace () in the place where it might go wrong.

# err.py

Import PDB

s = ' 0 '

n = Int (s)

pdb.set_trace () # Running to here will automatically pause

Print (10/n)

When paused, the PDB debug mode is entered. P View variables, C continue running

1.6. IDE (Integrated environment)

The better Python IDE now has:

    • Visual Studio code:https://code.visualstudio.com/, you need to install the Python plugin.

    • pycharm:http://www.jetbrains.com/pycharm/

2 , expand Documents

Python logging module (http://www.cnblogs.com/dahu-daqing/p/7040764.html)


Python Learning note __8.2 debugging

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.