Do python programs need to be compiled

Is Python a compiled language or an interpreted language? Before answering this question, you should first figure out what is a compiled language and what is an interpreted language.

The so-called compilation and execution means that the source code is compiled and processed by the compiler to generate the target machine code, which is the binary code that the machine can run directly, and there is no need to recompile it next time it runs.

However, it is for a specific CPU system. These target codes can only be executed on a specific platform. If the program needs to run on another CPU, the code must be recompiled.

It does not have portability, but the execution speed is fast. Languages such as C and C++ are compiled languages.

The interpreted language is translated line by line into the target machine code during the code execution, and the next time it is executed, it still needs to be interpreted line by line. We can simply think that Java and Python are both interpreted languages.

Compiled type is equivalent to the chef directly prepares a table of dishes, and the customer comes and eats it directly, while the interpretation type is like eating hot pot. The chef washes the dishes and the customer needs to cook and eat by themselves.

In terms of efficiency, an interpreted language is naturally inferior to a compiled language. Of course, it is not absolute. The efficiency of JIT is very high.

The above is a simple and rude distinction between compiled language and interpreted language, but Python (here mainly refers to CPython) is not strictly interpreted language;

Because the Python code is compiled (translated) into intermediate code before running, each .py file will be converted into a .pyc file. .pyc is a kind of bytecode file, which is platform-independent intermediate code. It can be executed on Windows or Linux platforms, and the virtual machine will translate the bytecode into object code line by line during runtime.

When we install Python, there will be a Python.exe file, which is the Python interpreter. Every line of Python code you write is executed by it. The interpreter consists of a compiler and a virtual machine. The compiler is responsible for translating the source The code is converted into a bytecode file, and the virtual machine is responsible for executing the bytecode. Therefore, the interpreted language actually has a compilation process, but this compilation process does not directly generate the target code, but the intermediate code (bytecode), and then Then use the virtual machine to interpret and execute the bytecode line by line.

to sum up

Python code will first program a bytecode file, and then be interpreted line by line by the virtual machine, and each line of bytecode code will be translated into target instructions for CPU execution.

Knowledge point expansion:

Python-based byte compilation

definition:

The process of defining a module as a binary language program is called byte compilation

Python is an interpreted language, and its byte compilation is done by the interpreter

The method of compiling py files and generating files ending with pyc,

Import zipfile.py

So far this article on whether python programs need to be compiled is introduced. For more related python programs, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

Do python programs need to be compiled
Does Python code need to be indented
What software do I need to install to learn Python?
Python tricks and tricks-continue to be updated...
How to write win programs in python
01. Introduction to Python
Introduction to Python
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
What can Python do
Do you still know how to draw cakes in python? ? ?