Analysis of Python interpretation and execution principles

Source: Internet
Author: User

Analysis of Python interpretation and execution principles

This article gives a detailed analysis of the implementation principles of Python, which can be helpful for a deep understanding of Python. The specific analysis is as follows:

First, the explain Execution here is relative to the compilation execution. We all know that a program written in C/C ++ or another compilation language needs to be converted from the source file to the machine language used by the computer, A binary executable file is formed after the link of the linker. When running this program, you can load the binary program from the hard disk to the memory and run it.

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 to bytecode, and then the python interpreter executes these bytecode. In this way, python does not have to worry about program compilation, Library Link loading, and other issues.

Python has the following three features:

1. Every time you run the command, you need to convert it into bytecode. Then, a virtual machine can convert the bytecode into a machine language before running the command on the hardware. Compared with the compilation language, the performance will certainly be affected when the compilation and link process is added at a time.

2. Because you don't need to worry about program compilation and library links, development is easier.

3. The python code and the machine underlying layer are far behind. The python program is easier to transplant and can be run on multiple platforms without any changes.

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

1. Language of compilation implementationSuch as C, C ++, Fortran, Pascal, and Ada. A source program compiled in a compiled language must 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 commands and cannot be run independently, because some assembler programs may not be able to interpret referenced library functions in the source program, and the library functions are not in the source program, at this time, you also need to link the program to complete the link task of external reference and target template call, and finally output executable code.

2. interpreted languageThe interpreter generates intermediate code instead of the target machine code. This intermediate code is different from the machine code. The intermediate code interpretation is supported by the software and cannot be directly used on the hardware. This software interpreter usually results in lower execution efficiency. Programs Written in interpreted languages are executed by another interpreter that can understand intermediate code. Different from the compiled program, the task of interpreting the program is to interpret the source code statements one by one as executable machine commands without translating the source program into the target code for execution. For an interpreted language, a dedicated interpreter is required to execute the program. Each statement can be translated only when it is executed, therefore, the efficiency is low.

3. Java interpreterJava is very special. java needs to be compiled, but it is not directly compiled into the machine language. Instead, it is compiled into bytecode and then executed in the java Virtual Machine in an interpreted way. Python also uses a similar method. First, it is compiled into a python bytecode, and then a special python bytecode interpreter is responsible for interpreting and executing the bytecode.

4. python is an interpreted language, but it provides a compilation method for efficiency considerations. After compilation, the pyc file is obtained and the bytecode is stored. Python is similar to java, but what is different from python is that python is an interpreted language, so compiling bytecode is not a mandatory operation. In fact, compilation is an automatic process and generally does not care about its existence. Compiling the code into bytecode can save the loading module time and improve the efficiency.

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

I believe that this article will help you understand Python in depth.


Explain the execution process of this Python Function

It is indeed recursive. When executing the command, one yield is passed to another yield, which seems inefficient.
This is similar to the example given at the end of this article:
Www.ibm.com/..rm-20/

In other words, listing = [I for I in fun] seems to be a List = List (fun)

How does one use python?

How Python interprets execution
 
The explain Execution here is relative to the compilation execution. We all know that a program written in C/C ++ or another compilation language needs to be converted from the source file to the machine language used by the computer, A binary executable file is formed after the link of the linker. When running this program, you can load the binary program from the hard disk to the memory and run it.
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 to bytecode, and then the python interpreter executes these bytecode. In this way, python does not have to worry about program compilation, Library Link loading, and other issues.
Python has the following three features:
Each operation must be converted to a bytecode, and then a virtual machine can convert the bytecode to a machine language to run on the hardware. Compared with the compilation language, the performance will certainly be affected when the compilation and link process is added at a time.
Because you don't have to worry about program compilation and database links, it's easier to develop your applications.
The python code and the underlying machine are far behind. The python program is easier to transplant and can be run on multiple platforms without any changes.
To implement a language on a specific computer, you must first determine the virtual computer that represents the semantic interpretation of the language, A key issue is whether the machine language on the actual computer or the machine language of the virtual machine during program execution. This problem determines the implementation of the language. According to the answer to this question, the programming language can be divided into two categories: compiled language and interpreted language.
Compiled and implemented languages, such as C, C ++, Fortran, Pascal, and Ada. A source program compiled in a compiled language must 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 commands and cannot be run independently, because some assembler programs may not be able to interpret referenced library functions in the source program, and the library functions are not in the source program, at this time, you also need to link the program to complete the link task of external reference and target template call, and finally output executable code.
In interpreted language, the interpreter does not generate the code of the target machine, but generates intermediate code. This intermediate code is different from the machine code. The explanation of intermediate code is supported by the software, it cannot be used directly on hardware. This software interpreter usually results in lower execution efficiency. Programs Written in interpreted languages are executed by another interpreter that can understand intermediate code. Different from the compiled program, the task of interpreting the program is to interpret the source code statements one by one as executable machine commands without translating the source program into the target code for execution. For an interpreted language, a dedicated interpreter is required to execute the program. Each statement can be translated only when it is executed, therefore, the efficiency is low.
Java interpreter. java is very special. java needs to be compiled, but it is not directly compiled into the machine language. Instead, it is compiled into bytecode and then executed in the Java Virtual Machine in the interpreted way. Python also uses a similar method. First, it is compiled into a python bytecode, and then a special python bytecode interpreter is responsible for interpreting and executing the bytecode.
Python is an interpreted language, but it provides a compilation method for efficiency considerations. After compilation, the pyc file is obtained and the bytecode is stored. Python is similar to java, but what is different from python is that python is an interpreted language, so compiling bytecode is not a mandatory operation. In fact, compilation is an automatic process and generally does not care about its existence. Compiling it into bytecode can save... the remaining full text>

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.