The Python programming language is easier for experienced developers to get started with and has powerful functions. It is an object-oriented general programming language, it helps us easily fulfill various functional requirements. Today, we will introduce the debugging methods of Python PDB.
- Analysis on how Python connects to SqlServer
- How to Implement the Socket service using Python
- Learn about the ease-of-use features of sending emails in Python
- Python dictionary addition and deletion operations
- A variety of common Python dictionary Application Methods
Programmers who have used C language are certainly familiar with GDB. Its single-step debugging and other functions are very important for us who often need debugging, python, as a dynamic language, has some difficulties in single-step debugging. However, the Python standard module PDB solves this problem better according to GDB. this article briefly introduces how to use PDB for single-step debugging.
What is Python PDB?
According to the definition in the Python document, PDB is a module that provides an interactive source code debugging function for Python programs. Its main features include:
Conditional breakpoint
Line-level single-step debugging of source code
View stack fragments
Evaluate arbitrary code (similar to GDB) in any stack fragment Environment)
Code list
Others
Simple Debugging Using Python PDB
Python programs can also implement single-step debugging functions similar to c/c ++, while PDB in Python is similar to common GDB commands in c.
The following is an example (debug_demo.py ):
- #! /Usr/bin/env Python
- _ Debug = True
- Def debug_demo (val ):
- If _ debug:
- Import pdb
- Pdb. set_trace () # introduce related pdb modules
- If val <10:
- Print "less than 10"
- Elif val <20:
- Print "less than 20, but big than 10"
- Else:
- Print "other condition"
Perform debugging in the command line as follows:
- Python debug_demo.py
In this case, you will enter the gdb-like interface to start related debugging.
- > /home/tower/test/Python/debug.py(9)debug_demo()
- -> if val <= 10:
- (Pdb)
You can enter h to view available command sets, which is similar to gdb ). Single-step debugging is very helpful in some situations.
Conclusion
Single-step debugging makes sense for the logic tracking of key parts of the program, so Python PDB is similar to GDB in C/C ++. however, compared with GDB, PDB functions are much weaker, so it is impossible to use all the feature of GDB in PDB. it is expected that the PDB will be improved as Python is upgraded to version 3.0 and later. it is worth looking forward.