General expressions in Python virtual machines (ii)

Source: Internet
Author: User

Creation of complex built-in objects

In the previous chapter of the Python virtual machine, in the general expression (a), we see how Python creates an empty Dictionary object and list object, so how does python behave if you create a non-empty Dictionary object and list object? demo2.py contains a Dictionary object and a list object, both of which contain elements at initialization, first we look at the symbol table and the constant table in the corresponding pycodeobject

# cat demo2.py i = 1s = "Python" D = {"1": 1, "2": 2}l = [1, 2]# python2.5......>>> Source = open ("demo2.py"). Read () &G T;>> CO = compile (source, "demo2.py", "exec") >>> co.co_names (' I ', ' s ', ' d ', ' l ') >>> co.co_consts (1, ' Python ', ' 1 ', 2, ' 2 ', None)

      

Secondly, we use the DIS module to explain the byte code corresponding to the demo2.py.

>>> Import dis>>> Dis.dis (CO) 1 0 load_const 0 (1) 3 store_name          0 (i) 2 6 load_const 1 (' Python ') 9 Store_name 1 (s) 3             Build_map 0 dup_top Load_const 0 (1)                     Rot_two load_const 2 (' 1 ') STORE_SUBSCR                          Dup_top Load_const 3 (2) Rot_two  Load_const 4 (' 2 ') STORE_SUBSCR Store_name 2 (d)               4 Load_const 0 (1) Load_const 3 (2) build_list 2 Store_name 3 (L) Load_const 5 (None)     Eturn_value

    

Now, let's analyze how Python virtual machines create Dictionary objects and list objects that contain elements

The first is the Dictionary object:

D = {"1": 1, "2": 2}//analysis Results 1 build_map                0 dup_top              load_const               0 (1) rot_two              load_const               2 ( ' 1 ') store_subscr         dup_top              load_const               3 (2) rot_two              load_const               4 (' 2 ') store_subscr< C10/>33 store_name               2 (d)

    

Build_map will create an empty dictionary and press it into the runtime stack, there's nothing to say, we'll look at the instructions after Build_map Dup_top

Ceval.c

Case dup_top:v = TOP (); Py_incref (v); PUSH (v); goto Fast_next_opcode;

  

Dup_top gets the top element of the stack, adds its reference, and then pushes the top element of the stack into the stack, and then the load_const instruction pushes the 1 object into the runtime stack, so let's look at the distribution of the runtime stack and namespace after the first 3 instructions have been executed:

Figure 1-1

The Python virtual machine then executes the instruction Rot_two, and we'll take a look at what this directive does:

Ceval.c

Case rot_two:v = TOP (); w = SECOND (); Set_top (w); Set_second (v); goto Fast_next_opcode;

  

Rot_two is also calling other macros to complete the task, let's take a look at the instructions for these macros:

Ceval.c

#define TOP () (stack_pointer[-1]) #define SECOND () (stack_pointer[-2]) #define SET_TOP (v) (stack_pointer[-1] = (v)) # Define Set_second (v) (stack_pointer[-2] = (v))

  

The original rot_two This instruction takes the first element and the second element of the runtime stack, then switches its position, and after the execution of Rot_two, we look at the distribution of the runtime stack and namespaces:

Figure 1-2

Then a load_const instruction was executed, the string ' 1 ' was pressed into the runtime stack, and then the STORE_SUBSCR was executed, and it was this instruction that set the mapping between the string "1" and the integer value 1 on the previous Dictionary object, let's take a look at store_ Content of SUBSCR:

Case store_subscr:w = TOP (); v = SECOND (); u = Third (); Stackadj ( -3);/* V[w] = u */err = Pyobject_setitem (V, W, u); Py_decref (U); Py_decref (v); Py_decref (w); if (err = = 0) continue;break;

  

W is the string "1" that was pressed into the stack before, and V is the second element of the runtime stack from the top of the stack, the Dictionary object, and U is the first value in the stack, that is, the integer value 1, and the string "1" is established by Pyobject_setitem (V, W, u) And an integer value of 1 in the dictionary mapping relationship. Similarly, the string "2" and the integer value 2 are also based on the same bytecode established by the mapping relationship, and finally execute the Store_name bytecode, on the namespace to establish the symbol D and the Dictionary object mapping

After the Dictionary object is successfully created and the initial value is given, the list object is also created, and the list object presses the initial value into the runtime stack with Load_const, and then calls Build_list to press the elements in the stack into the list object, Build_ The contents of the list are explained in the general expression (a) in the previous chapter of the Python virtual machine and are not repeated here

L = [1, 2]//analysis results 4 load_const               0 (1) load_const               3 (2) build_list               2 store_name               3 (L)

  

General expressions in Python virtual machines (ii)

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.