Python bytecode and python bytecode
Python is inefficient at repeatedly splicing immutable sequences because a new object is generated each time. The interpreter needs to copy the elements in the original object to the new object first, then, add new elements.
However, CPython optimizes string operations because the + = operation on strings is too common. Therefore, during str initialization, additional expandable space is reserved, so that no additional copying is performed during incremental operations.
Use bytecode to study this process.
>>> s_code = 'a += "b"'>>> c = compile(s_code, '', 'exec')>>> c.co_codeb'e\x00\x00d\x00\x007Z\x00\x00d\x01\x00S'>>> c.co_names('a',)>>> c.co_consts('b', None)
The obtained bytecode is of the Bytes type. Here is a bit of Bytes knowledge.
Bytes type
B 'e \ x00 \ x00d \ x00 \ x007Z \ x00 \ x00d \ x01 \ x00S ', and B indicates the Bytes type. Bytes records data in the form of a binary byte sequence. Each character represents a byte (8 bits ). For example, the preceding e Indicates binary 0110 0101. Shows the ASCII code table.
However, not all bytes are displayed, and even some bytes cannot correspond to ASCII code (because the ASCII code only defines 128 characters, and one byte has 256 characters ). For example, the ASCII code corresponding to 0000 0000 is not displayed, and 0111 1111 does not have the corresponding ASCII code.
To indicate these unreadable bytes, the \ x symbol is introduced, which indicates that subsequent characters are in hexadecimal notation. For example, \ x00 indicates the hexadecimal 00, which is 0000 0000 of the binary.
So far, all bytes can be expressed.
Bytecode Analysis
Return to the start code. For ease of display, convert B 'e \ x00 \ x00d \ x00 \ x007Z \ x00 \ x00d \ x01 \ x00S' To a hexadecimal display.
>>> c.co_code.hex()'650000640000375a000064010053'
You can use the opcode. opname function to obtain the operation instructions corresponding to the operation code.
>>> import opcode>>> opcode.opname[0x65]'LOAD_NAME'
Therefore, the complete bytecode can be interpreted as (TOS is the top-of-stack, the top element of the stack ):
Byte: location, function 65: 0, LOAD_NAME0000: parameter, press the co_names [0] value, that is, the value of a, into the stack 64: 3, LOAD_CONST0000: parameter, press co_consts [0], that is, 'B', to stack 37: 6, INPLACE_ADD, TOS = TOS1 + TOS5a: 7, STORE_NAME0000: parameter, co_names [0] = TOS, assign the stack top value to a64: 10, LOAD_CONST0100: parameter 53: 13, RETURN_VALUE, Returns with TOS to the caller of the function
In fact, the dis function can directly obtain the readable bytecode:
>>> import dis>>> dis.dis(s_code) 1 0 LOAD_NAME 0 (a) 3 LOAD_CONST 0 ('b') 6 INPLACE_ADD 7 STORE_NAME 0 (a) 10 LOAD_CONST 1 (None) 13 RETURN_VALUE
Complete code:
s_code = 'a += "b"'c = compile(s_code, '', 'exec')c.co_codec.co_namesc.co_constsc.co_code.hex()import disdis.dis(s_code)
Very failed. Compared with the value assignment bytecode of string and tuple, we didn't see the string optimization...
The above is the knowledge about python bytecode. Thank you for your support for the help house.