4 ms·
I promise the 15 minutes it takes to learn to use the debugger will save years of your life
by stuntcoder 6y ago
I promise the 15 minutes it takes to learn to use the debugger will save years of your life
- atakan_gurkan 6y agoWhich debugger would you recommend?
- dbrgn 6y agoFor Python: ipdb. In your code, you can just drop in "import ipdb; ipdb.set_trace()" and the execution will stop there with an interactive prompt. Alternatively start the script through (i)pdb and set breakpoints. You can also use plain pdb ("import pdb; pdb.set_trace()"), which has the advantage that it comes with the Python stdlib, but the interactive prompt is less fancy (no history, no autocompletion, etc).
- gcarvalho 6y agoAdd `export PYTHONBREAKPOINT=ipdb.set_trace` to your .bashrc and "breakpoint()" will invoke ipdb by default.
- dbrgn 6y agoThat is a great trick, thank you!
- the_cat_kittles 6y agoi use this frequently! but print() is still handy and my go to
- gjulianm 6y agoNot necessarily. I know very well how to use a debugger, but nowadays I just prefer to use prints: it forces to you actually think about what's happening instead of just looking at it. It's also more useful in situations where putting a debugger actually changes behavior (high performance systems, parallel programs).
- timzaman 6y agoExactly. And prints are cross platform cross language etc.
- eonwe 6y agoI would expect print-debugging to change the behavior in such cases too. Usually writing to stdout is behind some synchronization so using print debugging will drop performance and make program less parallel.
- gjulianm 6y agoYou can change where to put the prints if they do change the behavior (which incidentally also helps in understanding what's happening). You can print only if a condition happens, or only before/after critical sections, or print counters that you compute in the fast path. With the debugger you're always taking the overhead in and breakpoints will always change the flow of the program.
- deleted 6y ago[deleted]
- strokirk 6y agoThe issue is that the debugger stops execution at the breakpoint, but most often I want to analyze multiple print statements together. The debugger and repl have their uses, but IMHO I am usually better served by adding a print and having the script automatically re-run itself.