Search code examples
pythoninterpreterexecution

What does it mean the interpreter directly executes code?


I have read this statement that the interpreter directly executes the code. But I am not sure I understand what it means. I have been trying to get a good article on the execution cycle of python code.

I understand that the python code is converted to byte code and fed to the interpreter. So what happens next? Can someone explain clearly the steps that goes into it, especially in relation between the byte code, interpreter, OS and CPU?

Something along the lines of...

  1. OS loads the python interpreter in main memory
  2. CPU fetches the instruction and performs ALU. Updates the memory.. etc

Edited for clarity: My basic confusion is if CPU is what is executing the code, then what is meaning of saying 'the interpreter executes the code'?


Solution

  • That statement is a massive hand-waving over the whole process. Here's how it goes, try to keep up:

    1. Python code is lexed and parsed into an abstract syntax tree (AST).
    2. The AST is translated into Python bytecode.
    3. The Python bytecode is loaded into the Python virtual machine.
    4. The Python virtual machine reads the bytecode and calls out to native functions as the bytecode requires/specifies.

    The CPU never sees the bytecode as anything but data, and it does not need to see it any other way.