In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how Python works when using print", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how Python works when using print".
As we all know, Python is an interpretive language.
The so-called "interpretive" is, of course, different from the compiled language represented by the C language. The compiled language needs to convert the entire program file into a binary file that can be executed directly by the machine, while the interpreted language is the behavior that the corresponding interpreter "interprets" and executes the code description line by line.
Therefore, for new contacts, interpretative languages like Python often need to execute the corresponding statements in order to find some obvious errors.
Then again, how does Python's interpreter "interpret" Python code?
In fact, similar to the execution mechanism of Java, Python also has its own virtual machine. And this virtual machine actually executes a kind of "bytecode".
There is still a process of "compiling" in the execution of Python programs: compiling Python code into bytecode.
Also, Python provides a module called dis, which is used to view and analyze the bytecode of Python.
1. Dis module
For example, the dis module has a function of the same name, dis, which can be used to disassemble objects in the current namespace into bytecode.
Import disdef add (add_1, add_2): sum_value = add_1 + add_2dis.dis (add)
The execution result is:
4 0 LOAD_FAST 0 (add_1) 2 LOAD_FAST 1 (add_2) 4 BINARY_ADD 6 STORE_FAST 2 (sum_value) 8 LOAD_CONST 0 (None) 10 RETURN_VALUE
Where the first number "4" indicates that the content of the bytecode corresponds to the content of line 4 in the script.
A subsequent column of numbers indicates the address of the corresponding instruction. A rule can be found by vertical observation: the address of the next instruction is always 2 larger than that of the previous instruction. Is that a coincidence?
Apparently not. The changes recorded in the official document "dis-Python bytecode disassembler" show that since Python version 3.6, "each instruction uses 2 bytes." So the address of each instruction will be added 2 to the address of the previous instruction.
Then there is a list of word combinations that represent the meaning of instructions, which are actually human-readable corresponding instruction names. As the name implies, LOAD_FAST is to load some content / object somewhere, and "FAST" probably means that this is a quick and convenient command implementation.
On the far right is the Operand corresponding to the current command, that is, the Operand. A number is also a representation similar to an address, and the string in parentheses indicates the specific name of the corresponding object in the Python code.
So we can roughly read the generated bytecode:
First, Python loads add_1, the first parameter of the function add, into somewhere, followed by the second parameter, add_2, into the first argument. Then an instruction called BINARY_ADD is called to add the two parameters that were previously loaded. Then the addition and sum_value are stored in another location. Finally, a constant None is loaded and returned.
In fact, after reading the above execution process, it is easy to think of a commonly used data structure-stack.
Of course, this is not the focus of this article-if you really want to explore the implementation mechanism of Python, you will have to write a few more long articles to talk about it.
Using the dis.dis function, you can not only view the bytecode corresponding to each object in the current script, but also pass in a string corresponding to the code for disassembly:
# test_dis.pyimport diss = "" def add (add_1, add_2): sum_value = add_1 + add_2print ("Hello World!") import sys "" dis.dis (s)
Compilation results:
2 0 LOAD_CONST 0 () 2 LOAD_CONST 1 ('add') 4 MAKE_FUNCTION 0 6 STORE_NAME 0 (add) 5 8 LOAD_NAME 1 (print) 10 LOAD_CONST 2 ('Hello Worldwide') 12 CALL_FUNCTION 1 14 POP_TOP 7 16 LOAD_CONST 3 (0) 18 LOAD_CONST 4 (None) 20 IMPORT_NAME 2 (sys) 22 STORE_NAME 2 (sys) 24 LOAD_CONST 4 (None) 26 RETURN_VALUE2. Compile function
In addition to giving the string formed by the program to be disassembled directly in the program, we can also use the built-in function compile to form the compiled object of the corresponding script, and then use dis.dis to view the bytecode contents.
# test_compile.pyimport diswith open ("test_dis.py", "r", encoding= "utf-8") as f: s = f.read () compile_obj = compile (s, "test_dis.py", "exec") dis.dis (compile_obj)
Bytecode output result:
1 0 LOAD_CONST 0 (0) 2 LOAD_CONST 1 (None) 4 IMPORT_NAME 0 (dis) 6 STORE_NAME 0 (dis) 11 8 LOAD_CONST 2 ('\ ndef add (add_1) Add_2):\ nsum_value = add_1 + add_2\ n\ nprint ("Hello World!")\ n\ nimport sys\ n') 10 STORE_NAME 1 (s) 13 12 LOAD_NAME 0 (dis) 14 LOAD_METHOD 0 (dis) 16 LOAD_NAME 1 (s) ) 18 CALL_METHOD 1 20 POP_TOP 22 LOAD_CONST 1 (None) 24 RETURN_VALUE is the content of this article on "how Python works when using print" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.