Skip to content

Instantly share code, notes, and snippets.

@jacksmith15
Last active April 22, 2020 20:39
Show Gist options
  • Save jacksmith15/146ad52e668845094d4ff6f4a8810cf5 to your computer and use it in GitHub Desktop.
Save jacksmith15/146ad52e668845094d4ff6f4a8810cf5 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment