How Python programs run

Source: Internet
Author: User
1. Process overview Python first compiles the code (. py file) into bytecode and hand it over to the bytecode virtual machine. then the virtual machine executes the bytecode command one by one to complete program execution. 2. the bytecode corresponds to the PyCodeObject object in the Python virtual machine program .. Pyc files are the representation of bytecode on disks. 3. the creation time of the PyCodeObject in the pyc file is when the module is loaded, that is, import. Pyt... 1. Process overview

Python first compiles the code (. py file) into bytecode and delivers it to the bytecode virtual machine. then, the virtual machine executes the bytecode command one by one to complete program execution.

2. bytecode

The bytecode corresponds to the PyCodeObject object in the Python virtual machine program.

The. pyc file is the representation of bytecode on the disk.

3. pyc file

The creation time of the PyCodeObject object is when the module is loaded, that is, import.

Python test. py will compile test. py into bytecode and explain the execution, but will not generate test. pyc.

If test. py loads other modules, such as import util, Python will compile util. py into bytecode, generate util. pyc, and then explain and execute the bytecode.

To generate test. pyc, we can use the Python built-in module py_compile/compileall for compilation.

When a module is loaded, if it exists at the same time. py and. pyc, Python will try to use. pyc, if. pyc compilation time is earlier. py, then re-compile. py and update. pyc.

4. PyCodeObject

The Python code compilation result is a PyCodeObject object.

Typedef struct {

PyObject_HEAD

Int co_argcount;/* Number of location parameters */

Int co_nlocals;/* number of local variables */

Int co_stacksize;/* stack size */

Int co_flags;

PyObject * co_code;/* sequence of bytecode instructions */

PyObject * co_consts;/* All constants */

PyObject * co_names;/* set of all symbol names */

PyObject * co_varnames;/* set of local variable names */

PyObject * co_freevars;/* set of variable names used for closure */

PyObject * co_cellvars;/* set of variable names referenced by internal nested functions */

/* The rest doesn' t count for hash/cmp */

PyObject * co_filename;/* name of the code file */

PyObject * co_name;/* module name | function name | class name */

Int co_firstlineno;/* start line number of the code block in the file */

PyObject * co_lnotab;/* ing between bytecode commands and row numbers */

Void * co_zombieframe;/* for optimization only (see frameobject. c )*/

} PyCodeObject;

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.