Source code for a computer , It's all unreadable , Computers know only binary instructions . therefore , Want the program to be executed , So naturally, we need to convert the source code into binary instructions first , That's machine code .
that , When to make the transition ?
Some languages require that the code must be converted in advance , This is a compiled language , The conversion tool used is called compiler , such as C Language 、C++. Some languages can be executed and transformed at the same time , Where it's used, it's transferred , This is interpretative language , The transformation tool used is called interpreter , such as python、javascript.
compiler
Compilation is from source code ( It's usually a high-level language ) To object code that can be executed directly by a computer or virtual machine ( Usually low-level language or machine language ) Translation process of .
Interpreter
Interpret the relatively high-level program code into the machine code that the computer can run directly .
Python Interpreter [1]
When python After the package is installed on the machine , It generates some components : Include at least one interpreter and a set of support libraries . Compiling python The code has to run in the interpreter .
Python The interpreter consists of a compiler (compiler) And a virtual machine constitute , The compiler is responsible for converting the source code into a bytecode file , The virtual machine is responsible for executing bytecode . therefore , Interpretive languages also have an implicit compilation process , However, the compilation process does not directly generate object code , Instead, generate intermediate code ( Bytecode ), Then the bytecode is interpreted and executed line by line through the virtual machine .
Write a print “Hello” A string of python Program :
print(‘Hello’)
Carriage return operation :
The above code actually goes through two steps [1]:
1. Compile the source code into “ Bytecode ”, Put the source code file xx.py Compile into a bytecode file xx.pyc.
2. take “ Bytecode ” Forward to “ virtual machine ”,“ virtual machine ” perform “ Bytecode ” Convert to binary language and run on computer hardware .
Execution process [2]:
[1]: https://www.jb51.net/article/250335.htm
[2]: 【Python】 Talking about Bytecode + virtual machine (Python Interpreter )_ Where is Wen Shao's blog -CSDN Blog