"Learn Python with me" Python explains the principle of execution

Source: Internet
Author: User

The interpretation here is performed in relation to the execution of the compilation. As we all know, a program written in a compiled language like C + + is a machine language that needs to be converted from a source file to a computer's use, and a binary executable is formed after the linker links it. When you run the program, you can load the binaries from the hard disk into memory and run them.

But for Python, the Python source code does not need to be compiled into binaries, it can run the program directly from the source code. When we run the Python file program, thepython interpreter converts the source code into 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 features:

    1. Each run is converted to bytecode, and then the virtual machine converts the bytecode into machine language, which can then be run on the hardware. Compared to a compiled language, each time the compilation and linking process is more, performance will certainly be affected.
    2. The work of development is much easier because you don't have to worry about the program's compilation and the link to the library.
    3. Python code is further away from the bottom of the machine, and Python programs are easier to port and run on multiple platforms with virtually no changes.

To implement a language on a specific computer, the first thing to determine is the 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 of the actual computer or the machine language of the virtual machine . This question determines the realization of language. According to the answer of this question, the programming language can be divided into two categories: compiled language and interpretive language .

  1. compiled implementation of the language , such as: C, C + +, Fortran, Pascal, Ada. A source program written in a 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 and cannot be run independently, because the source program may use some assembler does not interpret the referenced 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 target template invocation of the linked task, and finally 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 interpretation of the intermediate code is supported by the software, can not be used directly on the hardware. This software interpreter usually leads to inefficient execution, and programs written in interpreted languages are executed by another interpreter that understands the intermediate code. Unlike compiled programs, the interpreter's task is to interpret the source code statements as executable machine instructions, without having to translate the source program into the target code. For an interpreted language, a specialized interpreter is required to execute the program, and each statement can be translated only when it is executed, and this interpreted language is translated once per execution and thus inefficient.
  3. Java interpreter , Java is very special, Java is required to compile, but not directly compiled into machine language, but rather compiled into bytecode, and then on the Java Virtual machine in the interpretation of the way to execute bytecode. Python also uses a similar method of compiling Python into Python bytecode, which is then interpreted by a dedicated Python bytecode interpreter to interpret the execution byte code.
  4. Python is an explanatory language , but for efficiency reasons, it provides a way to compile. After compiling, we get the PYc file, which 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, in fact, compiling is an automatic process and generally does not care about its existence. Compiling into bytecode can save time and improve efficiency of loading modules.
  5. In addition to efficiency , the form of bytecode also increases the difficulty of reverse engineering, which protects the source code. This is only a certain degree of protection, anti-compilation is still possible.
Related Article

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.