What is debug__ in Python
The Python Debugger
The Python module pdb provides an interactive debugger for Python programs. It includes evaluating the source code in single steps, inspecting variables, executing any Python code at any point and the so-called “post-mortem debugging”.
Start the debugger
True to the motto: “Many roads lead to Rome” - there are different ways to start the debugger. Depending on where and what is to be debugged, the user decides when the Python interpreter “enters” the debugger.
From the command line
The debugger prompt is (Pdb).
If we call the debugger from the command line, it loads our script pdb_test.py and stops at the first instruction:
In the interpreter
If you experiment in the interactive interpreter, it is also helpful to use the debugger here via pdb.run or pdb.runeval to be able to start:
In most cases, the options mentioned above are only helpful if the developed program is manageable or if problems occur at the beginning of the execution. If you come across errors in larger programs or only later in the execution, it is advantageous to start the debugger in the program:
The function call pdb.set_trace () in line 6 the debugger starts when the instruction is reached:$ python pdb_test.py> ... / pdb_test.py (7) test () -> print "Hello!" (Pdb)
After a mistake
Debugging programs after a failure is called “post-mortem debugging”. pdb supports this through the functions pm () and post mortem().
Line 7 is hiding a AttributeErrorsince we actually self.the_output would have to spend. If the said line is called, the execution stops with the corresponding error. After that we can pdb.pm () call to start the debugger at the point at which the exception occurred:$ python Python 2.7.2 (default, Oct 27 2011, 01:40:22) [GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pdb_test >>> pdb_test.Output ("Hello!"). echo () Traceback (most recent call last): File "
The debugger understands a defined set of commands - all other statements are treated as Python code and executed in the context of the program. Even variables and functions can be changed during debugging. Entering an empty line repeats the last command entered.
w (here): Display of the position in the call stack and the next instruction to be executed:
l (is) [start [, end]]: Call without arguments shows 5 lines before and after the current line. With one argument, 5 lines are displayed before and after the specified line and with 2 arguments, the specified range is displayed:(Pdb) list 1 #! / Usr / bin / env python 2 3 -> import pdb 4 5 class Output (object): 6 7 def __init __ (self, output): 8 self.the_output = output 9 10 def echo (self ): 11 print self.output (Pdb) l 13 8 self.the_output = output 9 10 def echo (self): 11 print self.output 12 13 if __name__ == '__main__': 14 Output ("Hello!"). echo () [EOF] (Pdb) l 5, 14 5 class Output (object): 6 7 def __init __ (self, output): 8 self.the_output = output 9 10 def echo (self): 11 print self.output 12 13 if __name__ == '__main__': 14 Output ("Hello!"). Echo ()
u (p), d (own): Current frame in the call stack upwards (up, older) or below (down, newer) move:(Pdb) up> ... / pdb_test.py (14)
Execute step by step
s (step): Executes the current line and stops at the next function or line to be called in the current sequence:
n (ext): Behaves similarly to step, but does not stop at functions that are called by the statement to be executed:(Pdb) ...> ... / pdb_test.py (14)
return): Stops at the next return-Instruction:#! / usr / bin / env / python import random def randint (a, b): random_int = random.randint (a, b) return random_int if __name__ == '__main__': random.seed () print randint (0, 100) (Pdb) ...> ... / pdb_return.py (11)
a (rgs): Shows the arguments of the current function and
p expression: Evaluated expression and shows its output, for example the value of a variable:
Of course, the debugger also supports Breakpoints and jump - I might write something about that in a second part.
Further information can be found in the Python documentation and the absolutely brilliant treatise by Doug Hellmann, called Python Module of the Week, whose structure I have freely adopted here.
- How long will we live in 2075?
- What are some slow deaths
- What are the assets of Asif Zardaris
- How can an Android user use iMessage
- Why do cast iron pans rust
- How are psychometric tests created
- Why do you keep asking questions
- How many sacraments are there in Catholicism
- How does the school's WiFi monitor work
- Black tea can cause dark urine
- What are the different types of drivers
- What makes a person ticklish
- Billie Eilish is lazy
- Where can we see microbes
- What are some examples of gender stratification
- Can a dead rattlesnake bite you
- Are there real pictures of the sun
- What is the size of the semiconductor IP market
- Why does the president control foreign policy
- Why did the British colonize Uganda?
- Why don't people hunt with tranquilizers
- Why is Macbeth returning to the witches
- Why do you give presents to someone
- How can I extract NES music