PyMOTW: sys, Part 5: Tracing Your Program As It Runs

There are two ways to inject code to watch your program run: tracing and profiling. They are similar, but intended for different purposes and so have different constraints. The easiest, but least efficient, way to monitor your program is through a trace hook, which can be used for writing a debugger, code coverage monitoring, or many other purposes.

Read more at pymotw.com: sys