Python debug; print () and assertion (instance parsing two)

Source: Internet
Author: User
Tags assert integer division in python
Let's look at debugging in Python in the following article. Learn Something python debuggingand know how Python debugging works in Python programming.

Why do you want to debug

The probability that the program can be completed once and run normally is very small, not more than 1%. There will always be a variety of bugs to fix. Some bugs are simple, look at the error message and know that some bugs are complicated, we need to know which variables are correct, and which values are wrong, so a full set of debug programs is required to fix the bug. So how does python debug ? The answers will be given below.

The first method is simple and straightforward, which is to print () the variables that might be problematic to look at:

def foo (s):    n = Int (s)    print (' >>> n =%d '% n)    return 10/ndef main ():    foo (' 0 ') main ()

After execution, look for the printed variable value in the output:

$ python err.py>>> n = 0Traceback (most recent call last):  .... Zerodivisionerror:integer division or modulo by zero

The biggest disadvantage of using print () is that it has to be erased in the future, and that the program is full of print (), and the results will contain a lot of junk information. So, we have a second method.

The second approach is to use the assertion (assert) instead of the place where print () is used to assist in viewing:

def foo (s):    n = Int (s)        assert n! = 0, ' n is zero! '    Return 10/n        def main ():    foo (' 0 ')

The assert means that the expression n! = 0 should be true, otherwise, depending on the logic that the program runs, the subsequent code will definitely go wrong.

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

$ python Err.pytraceback (most recent):  ... Assertionerror:n is zero!

If the program is filled with assert, and print () compared to where to go. However, you can turn off assert with the-o parameter when you start the Python interpreter:

$ python-o Err.pytraceback (most recent):  ... Zerodivisionerror:division by Zero

When you close, you can see all the assert statements as pass.

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.