Debugging – அதாவது கணினியில் பிழைகளை கண்டு திருத்தம் செய்வது எப்படி ? பைத்தான் மொழியில் இது சற்று சகஜமானது : முழு விவரம் இங்கு.
What is Debugging ?
Computer programs don’t always work like how we want them to. So at times we need to stop the program in the middle of execution and inspect them. By doing that – looking at the variables, functions, statements/source code in the debugger – we can understand the problem better than before and by stepping through the source code we can understand the source of the error to arrive at a solution.
This may sound somewhat complex, but in practice its quite repetitive and you will get the hang of it. Its the equivalent of a software detective work, and it is surprisingly fun, and you keep getting better at it with more practice.
How to Debug Python Code ?
To debug python we use the python module ‘pdb‘ [read documents இங்கு]; pdb is named evocatively like the more famous, powerful gdb – GNU source debugger. The simple usage is to call your program throwing the error from the command line as follows,
$ python -m pdb myscript.py
Once you see the (Pdb) prompt you can do the following:
- Setup a breakpoint at a particular function, class or module
- Resume the program running and
- Wait for the program to enter the breakpoint code or hit an exception
- At this time Pdb will enter the breakpoint and give you options to inspect variable, function, the call stack, and step up or down the frames
- For exceptions caught by gdb, we can go through the same scope variables and source-stepping inspections only via the post-mortem execution
Finally, you can figure out the cause of the problem and fix it!
Bon Voyage. You are starting on a powerful journey to write cool software and fix buggy ones!
Goodluck, and Godspeed.