Well, this is time to talk about something I have been playing, the pdb standard module python provides. I'll talk in the context of py 3.7+ since there are differences that improve how to work with this.
Well, first of all, as you should found in this field and coding experiences and challenges. You have to deal with bugs, unexpected errors, or even worst, unexpected behavior which sometimes is most difficult to trace.
Here are some lights on how to use it, this is really useful if your life is coding :). Consider the following commands table.
Command | Description |
s | Execute the current line and stop at the first possible occasion. |
n | Continue the execution until the next line in the current function is reached or it returns. |
p | Shows the values of variables in context code. |
ll | List the whole source code for the current function or frame |
l | In contrast to ll, this command shows a shorter snippet of code. |
l. | If you pass the param . to this command, it will show you always 11 lines around the current line. |
b | Set a breakpoint, you can specify either a line number or function name where execution will stop. |
c | Continues execution until a breakpoint is found. |
q | Quit debugging and exit. |
Having seen the table above, then let's play a bit. See the following example:
After running python example1.py, pdb comes in place, you'll see three important elements:
- the file, and line number in which pdb stand at this moment
- line code to see the context in which we are
- the pdb command line waiting for a command
I entered ll command (see on the table it meaning), as you can realize it, it shows all the context of line 14, remember this command shows more lines to better understand the context of the code, also you always see the -> that shows you where you are. After that first command entered, I ran n which continues to the next line (15).
Comentarios
Publicar un comentario