GIYF ; RTFM
Wanna quickly figure out how you got to a particular point in a foreign codebase? Throw an exception there.
I use import ipdb; ipdb.set_trace() in python almost pathologically. It’s 1 step up from an exception cause it drops you to a REPL, which is super convenient.
import ipdb; ipdb.set_trace()
With 3.7 we now have breakpoint() as a built-in.
I’ve found the interactivity of the IPython debugger to be slightly better than the built-in one, but nearly all of my codebase is pinned to Python 3.5 for now.
Use export PYTHONBREAKPOINT=ipdb.set_trace where ipdb is available and you don’t have to change your code for different environments. (at least once you’re on 3.7)
I like to use IPython’s embed to achieve a similar goal.
Add in a call to embed() as a breakpoint and find yourself in a full-blown IPython session with your objects in scope.
I use binding.pry the same way in Ruby. Requires the pry gem, but it’s well worth it.
I’ve been using pudb in the same manner. It’s got a TUI and it’s simple to jump into the REPL from there.
Ooh, pudb looks really cool! Might switch to using that
Yeah, unless there’s a try catch higher in the callstack consuming everything.
Or unless you are in a minified React component with no source map.
I use this all the time but always feel a little dirty and imposterish. I’m glad my method has been validated by a stranger on an internet board!
For big monolithic web apps: if finding the backend code for an operation is less obvious than it should be, find some method that will almost always be called (in our app it’s a particular method that gets the user context for a request, but it could be various things) and breakpoint it.