def a_long_function():
... # do some stuff
# Something is broken around here, we want to poke around, so we add this:
import pdb;pdb.set_trace()
... # other stuff
When we run the code, it stops at the import pdb;pdb.set_trace()
and gives us control over the program. We can rewrite the code as its executing!
Suppose there is a variable x that we think is the culprit. We run the debugger and inspect x
>>> # Debugger starts here
>>> x
< instance of SomeTypeName at a24cckn >
>>> type(x)
SomeTypeName
>>> vars(x) # Shows all public attributes and methods in a dict
{
"y": 1,
"z": None,
}
This is not too different to the IDE debugger, the difference is we can test possible solutions inline as well:
>>> x = < some other value >
>>> continue # code will continue executing from where it left off