Python Internal Execution process

Source: Internet
Author: User

I. Overview of the compilation process

When we execute the Python code, in the Python interpreter we use four processes to "disassemble" our code, which is eventually returned to the user by CPU execution.

First, when the user type code to the Python processing will be the first lexical analysis, such as the user typed the keyword or when the input keyword is wrong, will be triggered by lexical analysis, the incorrect code will not be executed.

Next Python will parse the syntax, for example, if the colon after test is written as a different symbol in the "for I in Test:", the code will still not be executed.

Following the most critical process, before executing Python, Python generates the. pyc file, which is the byte code, if we accidentally modified the bytecode, Python next recompile the program will be compared with its last generated bytecode file, if the mismatch will be modified bytecode file overwrite, to ensure the accuracy of the bytecode after each compilation.

So what is a byte code? The byte code in the Python virtual machine program corresponds to the Pycodeobject object. The. pyc file is the representation of the bytecode on disk. Simply put, in the process of compiling the code, the functions, classes and other objects in the code are processed first, and then the bytecode file is generated. With the bytecode file, the CPU can directly identify the bytecode file for processing, and then Python can execute it.

Second, the process diagram

Third, compile the byte code

Python has a built-in function compile (), you can compile the source file into CodeObject, first look at the description of this function:

Compile (...) compile (source, filename, mode[, flags[, Dont_inherit]), code object

Parameter 1: Content string for source file

Parameter 2: Source file name

Parameter 3:exec-compile module,single-compile a declaration, eval-compile an expression generally using the first three parameters is enough

Examples of Use:

#src_file. Py#some functiondef F (d=0):    c=1     print "Hello" a=9b=8f () >>> a=open (' src_file.py ', ' R '). Read ()    #命令行模式中打开源文件进行编译 >>> co=compile (A, ' src_file ', ' exec ') >>> type (CO) <type ' code ' >    #编译出了codeobject对象
Iv. Properties of CodeObject objects

What are the variables of codeobject, and then analyze the contents of the previous section?

>>> Print co.co_names    #所有的符号名称 (' f ', ' a ', ' B ') >>> print co.co_name    #模块名, function name, class name <module >>>> print co.co_consts    #常量集合, function f, and two int constants A,b,d (0, <code object f at 0xb7273b18, file ' Src_file ', line 2>, 9, 8, None) >>> print co.co_consts[1].co_varnames    #可以看到f函数也是一个codeobject, prints local variables in f (' C ',) >> > Print co.co_code    #字节码指令dZdZdZedS >>> print Co.co_consts[1].co_firstlineno    #代码块在文件中的起始行号2 > >> print co.co_stacksize    #代码栈大小2 >>> print co.co_filename    #文件名src_file    #模块名, function name, class name

CodeObject's Co_code represents a byte code, what does this bytecode mean? We can use the DIS module for the anti-compilation of Python:

Import Disdis.dis (CO) >>> output 2        0 load_const               0 (0)          3 load_const               1 (<code object F at 0xb727 3b18, file "Src_file", line 2>)          6 make_function            1          9 store_name               0 (f) 5        Load_const              2 (9) c10/>15 store_name              1 (a) 6        load_const              3 (8)          store_name              2 (b) 7        load_name               0 ( f)          call_function           0          pop_top                       load_const              4 (None)          Return_value

From the results of the anti-compilation, Python bytecode is actually a parody of the x86 assembly, the code is compiled into a single instruction to a virtual CPU to execute.

    • First column: line number
    • Second column: The offset of the instruction in the code block
    • Column three: directives
    • Fourth column: Operands
    • Column Fifth: Description of operands

Python Internal Execution process

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.