Python explains the execution principle analysis _python

Source: Internet
Author: User

In this paper, the principle of Python interpretation is analyzed in detail, which can help to understand python in depth. The specific analysis is as follows:

First, the interpretation performed here is relative to the compilation execution. As we all know, programs written in a compiled language such as C + + are required to convert from a source file to a machine language used by a computer, and a binary executable is formed after a linker link. When you run the program, you can load the binaries from your hard disk into memory and run them.

But for Python, Python source code does not need to be compiled into binary code, it can run programs directly from source. When we run the Python file program, the Python interpreter converts the source code to bytecode and then executes the bytecode by the Python interpreter. In this way, Python does not have to worry about program compilation, library link loading and so on.

For the Python interpretation language, there are 3 aspects of the following features:

1. Each run will be converted to bytecode, and then a virtual machine to the bytecode into machine language, the last to run on the hardware. Compared to the compiled language, the performance is certainly affected by the process of compiling and linking more than once each time.

2. The development of the work will be more relaxed without concern for the compilation of the program and the link to the library.

3.python code is farther from the bottom of the machine, and Python programs are easier to migrate and can be run on multiple platforms without any changes.

To implement a language on a specific computer, the first thing to determine is a virtual computer that represents the semantic interpretation of the language, and a key question is whether the basic representation of the program execution is the machine language on the actual computer or the machine language of the virtual machine. This problem determines the realization of language. According to the answer to this question, the programming language can be divided into two main categories: the compiled language and the explanatory language.

1. Compile the implementation of the language , such as: C, C + +, Fortran, Pascal, Ada. The source program written by the compiled language needs to be compiled, compiled, and linked to output the target code, and then the machine executes the target code. The target code is composed of machine instructions, can not run independently, because the source program may use some assembler can not interpret the reference library functions, 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 target template calls linked tasks, finally to output executable code.

2. Interpreted language , the interpreter does not produce the target machine code, but produces intermediate code, which is different from the machine code, the intermediate code interpretation is supported by the software, can not be used directly on the hardware. The software interpreter usually leads to lower execution efficiency, and programs written in interpreted languages are executed by another interpreter that understands the intermediate code. Unlike a compiled program, the task of an interpreter is to interpret the source code's statements into executable machine instructions one at a time, without having to translate the source program into the target code. For an interpreted language, a specialized interpreter is needed to execute the program, and each statement can be translated only if it is executed, and the interpreted language is translated once every time, thus being inefficient.

3.Java Interpreter , Java is very special, Java is required to compile, but not directly compiled into the machine language, but compiled into bytecode, and then on the Java Virtual machine in the interpretation of the execution of bytecode. Python also uses a similar approach, first compiling python into Python bytecode, and then a specialized Python bytecode interpreter that interprets the execution bytecode.

4.python is an interpretive language, but it provides a compilation method for efficiency reasons. After compiling, the PYc file is obtained and the byte code is stored. Python is very similar to Java, but Java is different from Python, Python is an interpreted language, so compiling bytecode is not a mandatory operation, in fact, compilation is an automatic process, generally do not care about its existence. Compiling bytecode can save time for loading modules and improve efficiency.

5. In addition to efficiency, the format of bytecode also increases the difficulty of reverse engineering, you can protect the source code. This is only a certain degree of protection, decompile is still possible.

I believe this article will help you to understand python in depth.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.