How Python is executed, byte code bytecode

Source: Internet
Author: User

Here is a blog about Python internal mechanism, already have some Chinese translation.

Maybe because I used Python 3.5, the example runs a little differently.

In addition, I have looked at some other references and summarized the following:

How Python is executed

Let's look at a more detailed step decomposition:

>>> a = "Hello"

After entering this line of code, when you press ENTER, Python performs a four-step operation:

1 lexing: Lexical analysis is the decomposition of a sentence into tokens. In general, it is a function that can be implemented with Str.split ().

2 parsing: Parsing is the assembly of these tokens into a logical structure.

3 Compiling: Compile, convert this logical structure into one or more code object

4 Interpreting: Explanation, execute the code represented by each code object .

There is one more simple way to say this:

The execution of the Python program is that it first compiles the code into bytecode (bytecode) instructions to the virtual machine and executes the bytecode instruction one by one.

These two statements are basically the same, but there is a difference between a code object and a bytecode . So what's the relationship between them?

In action,bytecode can be found in the properties of the code object .

Distinguish between function object, code object, and bytecode

>>> def Double (a):    return a*2>>> double<function double at 0x000001d8082e48c8>< /c11>

Why is pasting here aligned? Let's just ignore it.

As you can see from above, after defining a function, it becomes a function object. As long as you do not use the function call symbol-that is, the parentheses-this function will not execute.

However, it has been compiled and can be found by using the __code__ property of the function object to find its code object

>>> def Double (a):
Return a*2

>>> Double
<function double at 0x00000169c5f7ff28>

>>> type (double)
<class ' function ' >


>>> double.__code__ code object for #找到double function object

<code object double at 0x00000169c5f36ae0, file ' <pyshell#58> ', line 1>


>>> type (double.__code__)
<class ' Code ' >

As you can see from the last line, the type of code object is ' code '

As I said earlier,bytecode is the value of a property of code object . This property is named Co_code

Inside the Co_code property of the code object , a string is stored, which is the bytecode sequence:

>>> Double. __code__ . Co_codeb ' |\x00\x00d\x01\x00\x14s '

bytecode is a few meanings?

>>> Double. __code__ . Co_codeb ' |\x00\x00d\x01\x00\x14s '>>> type (double.  __code__. Co_code)<class'bytes'>>>> len ( Double. __code__ . Co_code) 8

Its type is ' bytes ' and the length is 8. You may find it strange how the 8 is counted.

Note: The str type in Python 3 is roughly the same as the Unicode type in Python 2, but the bytes type in Python 3 does not change the name of the STR type in Python 2.

Bytes is a binary sequence in which each element is an integer and the value is between 0-255.

 for  in Double. __code__ . Co_code:     Print (I, end="    " )    124    0    0    1    0    83   
>>> Double. __code__. Co_code[-1]

Is it exactly 8 elements? The first one is 124, the last one is 83.

>>> chr (124)'| '>>> chr 'S'

This is a confusing place: why mix characters such as ' | ', ' S ' with hexadecimal representations like x00? This is actually just python in the Display bytes type object to you, will put the ASCII code within the range of hexadecimal elements

The bytecode consists of 8 integers, each with a profound meaning, no less than the "eight villains" of Quentin

As you might have guessed, they are keys in a dictionary, and we need the values of the dictionary, which must be the name of the instruction.

This dictionary is in the file opcode.py.

 def_op ( load_const   ' , #   Index in const list  def_o P (  build_tuple    ", 102) #   def_op (  build_list   ", 103) #   Number of list items  def_op ( build_set   ", 104) #   Number of Set items  

So you guessed wrong, these numbers are the values in the key-value pairs, and we need the keys--they are really the names of the instructions.

There is a way to help you find the value (don't forget to import opcode first)

>>> opcode.opmap["load_fast"]124>>> opcode.opmap["  return_value"]83

Looking for value seems to mean nothing, what we need more is the method of finding the key.

>>> opcode.opname[83]'return_value'>>> opcode.opname[ 124]'load_fast'


In fact, there is no need to find a key, Python has a dis disassembly tool can be used (do not forget to import the dis first)
>>> Dis.dis (double)  2           0 load_fast                0 (a)              3 load_const               1 (2)              6 binary_multiply              7 return_value

Look back at those 8 integers (that's what bytecode means--the number that iterates over the bytecode with a for loop, representing a sequence of instructions)


124 0 0 1 0 83
Offset  0     1    2     3     4    5    6     7


So what's the 0 3 6 7 in the second column, that's the offset?

Write again tomorrow ~

How Python is executed, byte code bytecode

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.