Understand how Python executes, play with bytecode bytecode (bottom)

Source: Internet
Author: User

Last time, Python was executed by compiling the code into bytecode (bytecode) instructions, which were then executed by the virtual machine bytecode

And bytecode grew this way: B ' |\x00\x00d\x01\x00\x14s ' . Obviously this looks good for the machine, not for the human eye.

Although you can manually write this section of the bytecode in a dictionary, you can read it

But so tired of things, why do you have to do it yourself, so that your valet machine to do not just good.

The Python disassembly tool dis will do the job. The following is a brilliant purple to dis.dis the output of the interpretation of the results.

def double (a):     return a*2  import  dis>>> dis.dis (double)  2           0 Load_ FAST                0 (a)              3 load_const               1 (2)              6 binary_multiply              7 return_value

As explained at the end of the previous article, the number in column 2nd 0 3 6 7 is the offset of the bytecode.

The 3rd column is well understood, all opcode. Note that these opcode are for weak and weak human beings, not for machines, just look at B ' |\x00\x00d\x01\x00\x14s ' .

For example, the first opcode, the name called Load_fast, check the information, found that it means pushes a reference to the local Co_varnames[var_num] onto the stack.

To push a reference to a local object into the stack.

What is it? That's co_varnames[var_num].

(Inner OS: Look at this image as if it were a list or dictionary, the string is unlikely, the custom object is more unlikely ...)

>>> Double. __code__ Subscript 0 in column 4th  ' a  >>> Double. __code__. subscripts in column 4th of co_consts[1 12

Last time: Double is a function object.

DOUBLE.__CODE__ is the code object for this function object.

Look at the return value, a and 2, the 5th column .

so What is 2 of the 1th column? It looks like a mystery, but it's just a line number in the source code. In this example, the expression is in line 2nd of the double code.

The last time I explained it in detail, b ' |\x00\x00d\x01\x00\x14s ' is actually 8 integers

>>> Double. __code__ . Co_codeb ' |\x00\x00d\x01\x00\x14s '  for inch  Double. __code__ . Co_code:     Print (I, end="    " )    124    0    0    1    0    83    

by looking up a dictionary or another more ingenious and normal way, you can find out that 124 of the opcode represent Load_fast, 100 stands for load_const.

This type of opcode is followed by a two-byte parameter, 0 0 and 1 0, respectively.

But some opcode have no parameters behind them, such as the one represented by the 83 return_value

People who do not know may feel a little strange, why return_value without parameters, without parameters how to return the results?

Look up the information, return_value means Returns with TOS to the caller of the function. That is, the TOS is returned to the caller of the function. tos= top of Stack,. I'm out of the stack.

Now the question is: Why play with bytecode bytecode? It's so much easier to write Python code directly, why write a byte code?

Because you can save compilation time, here is a very detailed article, the author works in the field of genetic programming, found that their Python program total operation time, 50% of them are consumed by the compilation process. So the author went deep into the bytecode level to make a small change, drastically cut the compile time, gross position the operation time to less than half of the original.

Unfortunately did not find this article in Chinese translation. I wonder if there is anyone willing to pay me to translate.

The article writes:

After bytecode is written, we must let Python know that it is going to perform these bytecode. Then you need to create a complete code object.

Bytecode is the main ingredient of code object, but it requires something else to form a complete code object. Just like chicken is the main ingredient in Kung Pao chicken, but it can't be without peanuts.

Types is used in this process. CodeType ()

After you have the code object, you can then call Types.functiontype, which creates a function object.

On a written how cobwebs, follow the function object to find code object, and then find bytecode, here is completely upside down, embellish, upstream.

Understand how Python executes, play with bytecode bytecode (bottom)

Related Article

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.