Python study Notes-python debugging

Source: Internet
Author: User
When writing code, we often have a lot of errors. How should we debug it? When writing code, we often have a lot of errors. How should we debug it?


Print with print statement

We can print the desired content using the print statement and view it in the output.


Print "hah"

However, after debugging, we still need to manually delete the print statement, which is troublesome.


Assert

We can use the assert statement to replace the previous print statement. For example:

def foo(s):s = int(s)assert s != 0, "s is Zero"return 10.0 / sfoo('0')

The assert statement is followed by a judgment statement, and the error message is added. If the judgment statement does not match, an AssertionError is thrown. for example:

Traceback (most recent call last):File "/Users/W/Code/Python/Demo/AssertDemo.py", line 7, in foo('0')File "/Users/W/Code/Python/Demo/AssertDemo.py", line 3, in fooassert s != 0, "s is Zero"AssertionError: s is Zero

We can take the parameter-o to disable assert during execution. The assert statement does not take effect after it is disabled.


Logging

You can replace the print statement with logging. Logging does not throw error messages like assert. Logging has many advantages. One is that it can output specific levels of information.

Level: CRITICAL Numeric value: 50 Level: ERROR Numeric value: 40Level: WARNING Numeric value: 30 Level: INFO Numeric value: 20Level: DEBUG Numeric value: 10Level: NOTSET Numeric value: 0

We can use

logging.basicConfig(level=logging.DEBUG)

Perform simple configuration for logging. Warnings smaller than this level are ignored. In addition, you can configure the location of the logging output, for example, whether to output to the console or to a debug file. For more logging configurations, see https://segmentfault.com/a/11 ....


Debugger pdb, the python debugger

The pdb startup mode is


Python-m pdb test. py

Common pdb commands


N: next, used to execute the next step.
L: it should be list. check the code to be executed below.
P variable name: p should be the first letter of the parameter, view the value of a variable
Q: quit, exit the program.

Pdb can control the one-step execution of python. In theory, it is a omnipotent debugger. However, it is inefficient to process long codes. To analyze our requirements, we need to set a breakpoint at some key points. let's take a look at the execution results, rather than viewing each step as we did before. Next let's take a look at pdb. set_trace ().


Pdb. set_trace ()

We only need to write a line of code where the program is paused:


Pdb. set_trace ()

When the Python editor encounters pdb. set_trace (), the program will be suspended. we can use the pdb command mentioned above to view the values of each parameter.


Of course, many modern IDEs, such as Pycharm, provide a lot of convenient visual debug tools for easy use.

The above is the python learning notes-python debugging content. For more information, see PHP Chinese network (www.php1.cn )!

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.