In the last three months, Ned Batchelder and I spent a lot of time doing byterun. It is a Python bytecode interpreter written in Python. The process of doing byterun is very interesting and I have gained a lot of knowledge. At the end of this series of articles, I will try to convince you that you can also "play" byterun easily and happily. However, before that, we need to make some preparations: to understand how python works internally. In this way, we can understand what the interpreter can do and what it cannot do.
This series of articles is intended for readers who are similar to me three months ago. That is, you know python but have no idea about its internal work.
Tip: This article will be based on Python 2.7, and the interpreter in Python 3 is also very similar. Although I will ignore the syntax and naming differences between the two versions, my series can also be used in Python3.
How does Python work?
We will learn about the internal work of python from the high-level perspective. How does one work after you execute a line of code in your python REPL?
- ~ $ python
- Python 2.7.2 (default, Jun 20 2012, 16:23:33)
- [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
- Type "help", "copyright", "credits" or "license" for more information.
- >>> a = "hello"
When you press the return key, python completes the following four steps: lexical analysis, syntactic analysis, compilation, and interpretation. The work of lexical analysis is to break down the line of code you just entered into tokens. Note: tokens, keywords, numbers, operators, and so on ). The syntactic analysis program then receives these symbols and uses a structure to display the relationships between them. In this case, the abstract syntax tree is used ). The compiler then receives this abstract syntax tree and converts it into one or more) code objects. Finally, the interpreter receives these code objects one by one and executes the code they represent.
The main reason is that I do not know these steps, so I do not intend to explain lexical analysis, syntactic analysis, and compilation. However, we assume that these steps are running normally and can provide the interpreter with an appropriate python code object to complete the interpretation.
Before talking about code objects, I want to answer some common questions. In this series of articles, we will explain function objects, code objects, and bytecode. They are completely different concepts. Let's start with the function object. Although we don't need to know how function objects arrive at the interpreter, I want to emphasize that function objects and code objects are completely different. In addition, function objects are superb !).
Function object
You may have heard of function objects. When talking about "functions are the best objects" or "Python has the best functions", people will always mention it. Let's look at a function object.
- >>> def foo(a):
- ... x = 3
- ... return x + a
- ...
- >>> foo
- <function foo at 0x107ef7aa0>
"Function is the best object" indicates that a function is an object. It is like a list or an example: MyObject is an object. Since foo is an object, we can use it without calling it. That is to say, foo AND foo () are very different ). We can pass foo as a parameter to another function or assign a value to a new function named other_function = foo ). With such a great function, everything is possible!
In the second part, we will focus on the next level-code objects.
Link: http://akaptur.github.io/blog/2013/11/15/introduction-to-the-python-interpreter/
Http://blog.jobbole.com/55327/.