In a first series of posts I would like to explore how python works behind the scenes. I am going to re build some parts of Python in Python itself. My aim is not to reproduce PyPy, a project that is implementing a production grade Python interpreter in Python. Rather I think that by rewriting – probably simplified – parts of the interpreter, I can understand them better.
There are two main advantages of using Python for implementing parts of Python. One is that I am using a language that I am familiar with and like to program in. The second but very important advantage is that i can use Python to do all the stuff I have not (yet) rewritten. As we will see Python exposes a lot of its internals readily accessible from within Python programs.
These are the major parts of the Python interpreter.
Components of the Python Interpreter
- A compiler which produces bytecode, constant objects, lists of variable names and more from Python source code.
- An object model and built-in types, modules and functions.
- The interpreter itself, also known as a virtual machine, executing the bytecode and thereby modifying objects.
In my next posts I am going to write the third part, the virtual machine. The aim is to get it to run simple Python programs, and to understand how a virtual machine can be built.