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;