C vs. Python

A programmer writes standard-conforming C source code, and compilers targeting different platforms can translate it into machine instructions that their respective CPUs can execute.

What about Python? The key difference is:

C programs are usually translated into machine code ahead of time and then run.

With Python, we usually first install an environment capable of running Python programs, then give it our Python source code to execute. Such an environment normally includes a Python interpreter and the basic components it needs. The interpreter's machine instructions tell the CPU how to read Python bytecode, determine which operation it represents, and carry out that operation.

The following discussion uses CPython's conventional bytecode interpretation path as an example.

How does a CPU execute a Python program?

First, the Python compiler compiles our Python source code into bytecode. Although bytecode expresses “instructions,” from the CPU's perspective it is data read while running the Python interpreter. What the CPU ultimately executes is the interpreter's binary machine code. By executing the interpreter's machine instructions, the CPU reads bytecode, enters the corresponding handler, and produces a result.

We need to distinguish two roles here.

Python bytecodeThe interpreter's machine code
From the CPU's perspectiveDataInstructions
What interprets it directlyThe Python virtual machine's execution logicThe CPU
PurposeDescribes the operations a Python program needs to performImplements how to read, identify, and perform those operations

In this way, the CPU executes the interpreter's machine code to read bytecode, identify the required operations, and carry them out, achieving what we intended.

Connecting this process to what I have learned about CPUs

Suppose the interpreter's machine code starts at memory address 0x1000, while the Python bytecode is stored in a separate memory region.

Illustration: the CPU's PC points into the interpreter's machine code, while Python bytecode is read as ordinary data; addresses and bytecode layout are illustrative only

There are two positions to distinguish during this process: 1. The CPU's PC: the execution position within the CPU's machine-instruction stream. 2. The bytecode position maintained by the interpreter: the Python bytecode instruction currently being processed.

Suppose the current bytecode is ADD.

The CPU's PC does not jump directly to the memory address containing that bytecode and execute it as a CPU addition instruction.

Instead, the CPU's PC continues through the interpreter's machine code:

Read bytecode → Compare → Branch → Execute the addition handler

The result is then produced.

To bring this together, the CPU executes the machine instructions of the already-compiled Python interpreter. Following those instructions, it reads Python bytecode from memory and examines it as data. Comparisons, jumps, or calls lead to the appropriate handler in the interpreter. These handlers are also machine instructions: they read Python objects, perform computations, and save results.

Throughout this process, the CPU executes machine code, while Python bytecode is data processed by the interpreter.