[Edit: A significantly expanded version of this series appears as a chapter in The Architecture of Open Source Applications, volume 4, as A Python Interpreter Written in Python.]
When we left our heroes, they were examining a simple function object. Let’s now dive a level deeper, and look at this function’s code object.
1 2 3 4 5 6 7 8
As you can see in the code above, the code object is an attribute of the function object. (There are lots of other attributes on the function object, too. They’re mostly not interesting because
foo is so simple.)
A code object is generated by the Python compiler and intepreted by the interpreter. It contains information that this interpreter needs to do its job. Let’s look at the attributes of the code object.
1 2 3 4 5 6 7
There’s a bunch of stuff going on here, much of which we’re not going to worry about today. Let’s take a look at three attributes that are interesting to us for our code object on
1 2 3 4 5 6
Here are some intelligible-looking things: the names of the variables and the constants that our function knows about and the number of arguments the function takes. But so far, we haven’t seen anything that looks like instructions for how to execute the code object. These instructions are called bytecode. Bytecode is an attribute of the code object:
So much for our intelligible-looking things. What’s going on here? We’ll dive in to bytecode in Part 3.