Home > Backend Development > Python Tutorial > python study notes-python debugging

python study notes-python debugging

黄舟
Release: 2017-01-17 14:21:54
Original
1131 people have browsed it

When we write code, we often make a lot of mistakes. How do we debug this?


Print with print statement

We can use print statement to print what we want, and then view it in the output.


print "hah"

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


assert

Where print was used previously, we can use the assert statement instead. For example:

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

assert statement is followed by a judgment statement, and then the error message. If the judgment statement does not match, an AssertionError will be 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 foo
assert s != 0, "s is Zero"
AssertionError: s is Zero
Copy after login

We can turn off assert uniformly with the parameter -o during execution. After closing, the assert statement will no longer take effect.


logging

You can replace the print statement with logging. Logging does not throw error messages like assert does. There are many benefits of logging. One is that it can be customized to output a specific level of information.

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

We can use

logging.basicConfig(level=logging.DEBUG)
Copy after login

to simply configure logging. Warnings smaller than this level will be ignored. In addition, we can also configure the location of the logging output, such as whether it is output to the console or a certain debug file. For more logging configuration, you can read: https://segmentfault.com/a/11….


Debugger pdb, the python debugger

The startup method of pdb is


python -m pdb test.py

Commonly used commands for pdb


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 parameter, check the value of a certain variable
q: quit, exit the program

pdb can control the step-by-step execution of python. In theory, it is a universal debugger . But when dealing with very long codes, it seems inefficient. Analyzing our needs, we actually need to set breakpoints at some key points so that we can look at the execution results instead of looking at each step like 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 pause, and we can use the pdb command mentioned above Let’s check the values ​​of each parameter.


Of course, many modern IDEs such as Pycharm provide many convenient visual debugging tools, which can be easily used.

The above is the content of python study notes-python debugging. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template