How the first part of Python works

Source: Internet
Author: User

How Python Runs the program

When Python runs the script, the first step is to compile the so-called "bytecode" and then forward it to the "virtual machine".

1, bytecode compilation:

When the program executes, the inside of Python (which is completely hidden from most users) first compiles the source code (the statements in the file) into the form of so-called bytecode. Compilation is a simple translation step, and the bytecode is the underlying, platform-independent representation of the source code. In summary, Python translates these source statements into a set of bytecode directives by decomposing each source statement into a single step. These bytecode can improve execution speed: bytecode runs much faster than the original source code statements in a text file.

If the Python process has write permissions on the machine, it will save the program's bytecode to a file with a. pyc extension (". PYc is the compiled. py" source code). Python saves bytecode as an optimization for startup speed. The next time you run the program, if you haven't modified the source code since you last saved the bytecode, Python will load, pyc the file and skip compiling this step. When Python has to be recompiled, it automatically checks the timestamp of the source and bytecode files: If you save the source code again, the bytecode will be automatically recreated the next time the program runs.

If Python cannot write bytecode on the machine, the program can still work: the bytecode will be generated in memory and simply discarded at the end of the program.

2. Python virtual machine (PVM)

Once the program is compiled into bytecode (or bytecode is loaded from an existing. pyc file), the subsequent bytecode is sent to execute on a Python virtual machine (Python), which is commonly referred to as a VM. PVM is not a standalone program and does not require installation. In fact, PVM is a large loop that iterates through bytecode instructions and completes operations one after the other. PVM is a python run engine that often manifests itself as part of a Python system, and it is the component that actually runs the script. Technically, it's the last step in the so-called "Python interpreter."

Figure 2-2 describes the structure of the run-time described. These complexities are intentionally hidden from Python programmers. Bytecode compilation is done automatically, and PVM is only part of the Python system installed on the machine. Programmers simply write code and run files that include statements.

Attention:

(1) The meaning of performance

Pyhton differs from fully compiled languages such as C and C + +:

There is no "build" or "make" step in A.python's work, and the code runs immediately after it is written;

B.python bytecode is not a binary encoding of the machine (for example, an Intel chip Directive).

Python code cannot run as fast as C or C + + code because the PVM loop (not the CPU chip) still needs to interpret bytecode, and bytecode instructions require more work than CPU instructions.

Pure Python code runs faster than a traditional interpretation language because, unlike other classic interpreters, there are internal compilation steps: Python does not need to re-parse and re-decompose each line of statements over and over again.

(2) Meaning of development

Another case where Python executes the module is that the environment in which it is developed and executed is virtually indistinguishable. In Python, the compiler always appears at run time and is part of running the program system.

The only runtime you really have in Python is that you don't need the initial compilation phase at all, and everything happens when the program is running, even when you're building functions and classes and connecting modules, which are often the same thing that happens to static languages before they are executed.

How the first part of Python works

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.