How Python Works

Source: Internet
Author: User

Python is an interpreted language, when executing Python, the interpreter translates the source code in bytecode into byte code, and then the byte code is given to the Python virtual machine PVM to execute, as shown in the process:

BYTE code

1 byte code translation

The Python interpreter does not always translate source code into bytecode byte code. For example, if you do not make any changes to the source code, when you execute the Python program, the Python interpreter simply hands the bytecode to the Python virtual machine and skips the steps of translating the source code into bytecode. In the following two scenarios, the Python interpreter will re-translate the source code into bytecode:

1 The source code is changed. The Python interpreter checks the time stamp of the source code and bytecode, and if the source code is found to be modified, it will be re-translated.

2 python version changed. If the Python interpreter finds that the version information contained in the bytecode does not match the current Python version, it will be re-translated. For Python implementations prior to <python3.2, the version information is contained in a "magic" version number in the bytecode file, and for the Python implementation of >=python3.2, the bytecode file name contains the version information

2 byte-code storage location

The python bytecode suffix "pyc" represents the complied.py file. For <python3.2 implementation, the bytecode file is placed in the same directory as the source file, and for the implementation of >=python3.2, the bytecode file is placed separately under a __pycache__ folder, and the __pycache__ folder is in the same directory as the source file. And the bytecode files stored under __pycache__ contain Python version information, such as SCRIPT-33.PYC, which indicates that the current bytecode is translated by the Python3.3 implementation.

Pvm

Python virtual machines do not need to be installed separately, it is part of the Python implementation, essentially a large loop, a sentence to execute the code inside the bytecode

How 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.