Debugging Python

Debugging is one of the best skills you can learn, as it will help you in many difficult situations.

Every language has its debugger. Python has pdb, available through the standard library.

You debug by adding one breakpoint into your code:


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Debugging is one of the best skills you can learn, as it will help you in many difficult situations.

Every language has its debugger. Python has pdb, available through the standard library.

You debug by adding one breakpoint into your code:

breakpoint()

You can add more breakpoints if needed.

When the Python interpreter hits a breakpoint in your code, it will stop, and it will tell you what is the next instruction it will run.

Then and you can do a few things.

You can type the name of any variable to inspect its value.

You can press n to step to the next line in the current function. If the code calls functions, the debugger does not get into them, and consider them “black boxes”.

You can press s to step to the next line in the current function. If the next line is a function, the debugger goes into that, and you can then run one instruction of that function at a time.

You can press c to continue the execution of the program normally, without the need to do it step-by-step.

You can press q to stop the execution of the program.

Debugging is useful to evaluate the result of an instruction, and it’s especially good to know how to use it when you have complex iterations or algorithms that you want to fix.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-01-02T05:00:00+00:00) Debugging Python. Retrieved from https://www.scien.cx/2021/01/02/debugging-python/

MLA
" » Debugging Python." flaviocopes.com | Sciencx - Saturday January 2, 2021, https://www.scien.cx/2021/01/02/debugging-python/
HARVARD
flaviocopes.com | Sciencx Saturday January 2, 2021 » Debugging Python., viewed ,<https://www.scien.cx/2021/01/02/debugging-python/>
VANCOUVER
flaviocopes.com | Sciencx - » Debugging Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/01/02/debugging-python/
CHICAGO
" » Debugging Python." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/01/02/debugging-python/
IEEE
" » Debugging Python." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/01/02/debugging-python/. [Accessed: ]
rf:citation
» Debugging Python | flaviocopes.com | Sciencx | https://www.scien.cx/2021/01/02/debugging-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.