View Python byte code __python

Source: Internet
Author: User

1. How to view all of the Python bytecode lists:


Execute this code to see a list of bytecode:

    Import opcode  
    for op in range (Len (opcode.opname)):  
      print (' 0x%.2x (%.3d):%s '% (OP, op, Opcode.opname[op])  

Since it is called "byte code", these opcode are naturally in bytes, and can only represent 256 different opcode. Python actually uses only the Bailai opcode.
Operation code is less than 90 of the parameters, the instruction contains only the opcode itself, a total of 1 bytes, or greater than 90, then each instruction in the opcode followed by 1 parameters, the length of the parameter is 2 bytes, a total of 3 bytes.
The bytecode of the Python program is stored in a pystringobject form in the Pycodeobject co_code domain at run time. The Co_code domain contains only instructions and no other program data, and variable names, constants, and so on, are placed in other fields.
The Python byte code instruction set is a stack based instruction set. The "stack" here refers not to the function call stack, but to the stack that is dedicated to evaluation, which can be called the "Evaluation stack" (evaluation stack) or "operand stack" (operand stack). The temporary variables of the evaluation process are all placed on the evaluation stack, and most of the instruction set deals with the stack.
For example 3 + 4 will become:
Python bytecode Code Load_const 0 (3) Loat_const 1 (4) Binary_add
Suppose that the items labeled 0 and 1 in the constant pool are 3 and 4, respectively, of the two constants, then the first two instructions put these two constants into the evaluation stack, then the binary_add instruction will be evaluated stack top two values pop-up, calculate addition, and put the results again into the stack. With this in mind, the Python instruction set is basically the name. Operation code corresponding to the specific meaning can be found in the following website documents link.


2 Specific view of your program in the bytecode involved in the method:


1. View the bytecode generated by all functions in the module and functions in all classes:

Import Dis,target
Dis.dis (target)

2. View the byte code generated by a function in a function or class in the module:

Import Dis,target
Dis.dis (Target.funca)

Or

Import Dis,target
Dis.dis (Target.classa)

3. View operations outside the function:

S=open (' target.py '). Read ()
Co=compile (S, ' target.py ', ' exec ')
Import dis
Dis.dis (CO)


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.