Python explains performing principle analysis

  • 2020-04-02 13:57:22
  • OfStack

In this paper, the principle of Python interpretation and execution is analyzed in detail, which is helpful for further understanding of Python. Specific analysis is as follows:

First, the interpreted execution here is relative to the compiled execution. We all know that programs written in compilative languages such as C/C++ need to be converted from source files into the machine language used by the computer, which is then linked through the linker to form binary executables. When you run the program, you can load the binaries from the hard drive into memory and run them.

But for Python, the Python source code does not need to be compiled into binary code, it can run programs directly from the source code. When we run the python file program, the python interpreter converts the source code into bytecode, which is then executed by the python interpreter. This way, python doesn't have to worry about compiling programs, linking libraries, etc.

The python interpreted language has the following three features:

1. Every run is converted to bytecode, and then a virtual machine converts the bytecode to machine language before it can be run on hardware. Compared to a compiled language, every time there is more compilation and linking, performance is bound to suffer.

2. Development is made easier by not having to worry about compiling programs and linking libraries.

3. Python code is further from the bottom of the machine, and python programs are easier to port and run on multiple platforms with little change.

To implement a language on a concrete computer, the first thing to be determined is the virtual computer that represents the semantic interpretation of the language. A key issue is whether the basic representation of program execution is the machine language on the actual computer or the machine language of the virtual machine. This problem determines the implementation of the language. According to the answer to this question, Programming languages can be divided into two broad categories: compiled and interpreted.

1. Compile the implemented language , such as C, C++, Fortran, Pascal, Ada. Source programs written in a compiled language need to be compiled, assembled, and linked to output object code, which is then executed by the machine. The object code is composed of machine instructions and cannot be run independently, because some assembler may be used in the source program to explain the library function referenced, and the library function is not in the source program, at this time also need to link the program to complete the external reference and the link task of the target template call, finally output executable code.

2. Interpreted language Instead of producing target machine code, the interpreter produces intermediate code, which unlike machine code is interpreted in a way that is supported by software and cannot be used directly on hardware. This software interpreter usually results in inefficient execution, where a program written in an interpreted language is executed by another interpreter that can understand the intermediate code. Unlike a compiled program, the interpreter's job is to interpret the statements of the source code one by one into executable machine instructions, without translating the source code into object code for execution. For interpreted languages, a special interpreter is needed to execute the program, and each statement can only be translated when executed.

3. The Java interpreter Java is special. Java needs to be compiled, but it is not compiled directly into machine language. Instead, it is compiled into bytecode, which is then executed in an interpreted manner on the Java virtual machine. Python USES a similar approach, compiling Python into Python bytecode, which is then interpreted and executed by a dedicated Python bytecode interpreter.

Python is an interpreted language, but it provides a way to compile for efficiency. When compiled, you get a pyc file that stores the bytecode. Python is very similar to Java, but unlike python, python is an interpreted language, so compiling bytecode is not a mandatory operation. Compiling to bytecode saves time and efficiency in loading modules.

5. In addition to efficiency, the form of bytecode also increases the difficulty of reverse engineering, which can protect the source code. This is only a degree of protection, decompilation is ok.

I believe that this article will help you to further understand Python.


Related articles: