Python sys.settrace Hooks — ELI5

Imagine you have a marble run — one of those toys where a marble rolls down ramps, through tubes, and over bridges. Normally you just watch the marble come out at the bottom. But what if you could put a tiny camera at every turn so you could see the marble at each step?

That is what sys.settrace does for your Python code. It installs a “camera” that Python calls every time something interesting happens: a new line runs, a function is entered, a function returns, or an error is raised. Your camera function gets told what just happened and can record it, print it, or even change the route.

This is how debuggers like pdb work. When you set a breakpoint and step through code line by line, behind the scenes Python is calling a trace function at every step. The debugger’s trace function checks “is this a breakpoint?” and pauses if so.

You can also use it for code coverage tools. Ever wondered how tools know which lines of your code actually ran during tests? They install a trace function that keeps a tally mark for every line that executes. When tests finish, any line without a tally mark is uncovered code.

The catch is speed. Having a camera at every turn slows the marble down. Programs with tracing enabled run significantly slower — often two to ten times slower. That is fine for debugging and testing, but you would never leave it on in a live application.

The one thing to remember: sys.settrace lets you install a callback that Python invokes at every line, function call, return, and exception — it is the foundation of debuggers and coverage tools.

pythondebuggingintrospection

See Also

  • Python Ast Module Code Analysis How Python's ast module reads your code like a grammar teacher diagrams sentences — turning source text into a tree you can inspect and change.
  • Python Dis Module Bytecode How Python's dis module lets you peek at the secret instructions your computer actually runs when it executes your Python code.
  • Python Gc Module Internals How Python's garbage collector automatically cleans up memory you are no longer using — like a tidy roommate for your program.
  • Python Importlib Custom Loaders How Python's importlib lets you teach Python to load code from anywhere — databases, zip files, the internet, or even generated on the fly.
  • Python Site Customization How Python's site module sets up your environment before your code even starts running — the invisible first step of every Python program.