See CPython run: Getting to know your Python interpreter
By James Bennett

You might run a Python interpreter multiple times a day, but do you really know what it's doing while it runs your code? And if Python is doing something that seems strange, where might you start looking to figure out why? Come find out in this guided tour of how the CPython interpreter works!

Python is a pretty neat language, but without an interpreter it doesn't do much. And although lots of people use the CPython interpreter (which is the main implementation of the Python language) every day, its workings are still a mystery even to many experienced Python programmers. So rather than explain features of Python the language, this talk will cover the interpreter itself, how it runs your code, and how you can use knowledge of the interpreter to figure out why your Python code behaves the way it does.

To get started, we'll briefly look at some real questions asked on Python forums, wondering about strange, or at least unexplained, behaviors of Python. Like: Why does a different way of writing the same code sometimes run faster? Why does the behavior of the is operator seem to be inconsistent for some kinds of values? And what's the deal with all those ".pyc" files Python likes to leave all over the place? How and where would we start looking for answers to these?

Then we'll take a look at the overall life cycle of Python code. We'll start with Python source code, and see how Python parses it into an abstract syntax tree (AST), how you can ask Python to show you the AST for a given piece of code, and how to read and understand it. Next we'll look at the transformation from AST to Python bytecode, what a Python code object looks like and how it's built, and how you can access it and understand the bytecode. Finally we'll see how the CPython virtual machine runs the compiled bytecode, and what's going on as it does so.

Then we'll dive into the infrastructure of the interpreter, pointing out where to go to see things like the low-level implementations of Python's built-in types, the bytecode interpreter loop, the AST and bytecode optimizers, and more, along with tips on how the interpreter's source code is organized and how to find and read things you're interested in.

Finally we'll circle back to some of the original questions about Python behavior, and see how they can be answered by using knowledge of how the CPython interpreter works, along with tools and tactics for making it easier to dig into what Python's doing with your code, and references for further reading and learning.

James Bennett

Philosopher turned web geek. I like Django, Python 3, and the Oxford comma.

Twitter

Sponsors