#!/usr/bin/env python """Example of usage of ipython debugger (ipdb) to interactively debug something in the code with autocomplete and color support. You may need to install: sudo pip install ipdb """ def my_func(my_arg): # my_arg is a complex object that we don't know much about, maybe bad documentation or we are just lazy # Let's imagine we have some complex logic that has a problem in the middle of it for i in range(10): if i == 7: # You could print here and keep adding prints until you find what's wrong, # which can be time consuming and frustrating # You could also use your IDE's debugger (probably a better option), however, # sometimes that's challenging too print("There's an error here") # So another option is to add an embedded debugger straight in the code (built-in functionality) # import pdb; pdb.set_trace() # The above would be the default way, but if you want autocompletion and color goodies from ipython # I recommend the following (ignore fmt, it's for 'black' formatter to stop splitting into multiple lines) import ipdb; ipdb.set_trace() # fmt: skip # from IPython import embed; embed() # Now you can play around with the state, check what is my_arg, modify it, and use 'continue' # to let the code continue executing (or 'exit' to kill the program) # tip1: you can use 'locals()' to see all local variables and 'globals()' to see all global # tip2: you can use type() to know the type of something if __name__ == "__main__": # Let's call my_func with different arguments my_func({"thing": 1, 123: "woah"}) my_func(456) my_func(my_func)