A discussion of the Python interpreter: The first part of the function object
In the last three months, I andNed Batchelderspent a lot of time in developingByterun. This projectByterun(Https://github.com/nedbat/byterun) is usedpythondeveloped bypythonThe interpreter for the byte code. Development interpreterByterunprocess, let me learn a lot of things, and bring great pleasure. In this article series, I will take you to experience my development fun, and useByterunis very happy about things. However, before experiencing happiness, be aware of some basic knowledge, like warming up before exercising:pythonThe internal implementation has an overall understanding so that we know what the interpreter is and what it does and what it can't do.
This article assumes that you know that python is the same thing as I did three months ago, except that I don't know anything about it and don't say what python is doing inside.
It is noteworthy that the main concern and discussion in this paper is based on the python2.7 version. In fact , the python3.0 version of the interpreter is not too much, similar to the previous version. Mainly some syntax and naming is not the same, based on the underlying content can be applied to the python3.0 version.
How does python work?
We'll start with an advanced interactive interface to see how Python works inside. When you enter a line of code in the python interaction interpreter and then run it, what does the interpreter do?
~ $ python
Python 2.7.2 (Default, June 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 enter that line of code, when you press ENTER, There are four things that happen to the Python interpreter: Lexical parsing, parsing, compiling bytecode generation and interpreting execution bytecode. Lexical analysis is done by analyzing the code you entered, and then generating a single word (token). What the grammar analysis does is to parse the words and then generate a structure that is appropriate to express the relationship between the words based on the relationship between them (here is the abstract syntax tree of the AST generation ). The compile-generation code phase is to generate one or more code objects from the AST structure, and the code objects become bytecode and data. Finally, the execution phase is interpreted to run the bytecode in each code object.
In today's article, I'm not going to discuss lexical analysis, parsing, or compiling code, because I don't understand these stages, and I can't express them in my language. Instead, I assume that these are all running correctly and that you can generate the appropriate Python code object to run for the interpreter.
Before we discuss the code object, let's start with some comprehensive background information. In this article series, we will discuss function objects, code objects, and bytecode. The three are completely different, and then the function object is discussed first. Although not understanding the function object can also understand the internal functions of the interpreter, but I want to emphasize that the function object and code is two different things, function objects more special.
Function Object
You may often hear about function objects, because people often talk about them, such as "a function object is the most important type of object" or "python code is primarily composed of a function object". Let's start by looking at how the function object looks:
>>> def foo (a):
... x = 3
... return x + A
...
>>> Foo
<function Foo at 0x107ef7aa0>
"Function object is the most important kind of object" means that the function is an object, not just likeListobject orMyObjectobject is the same. BecauseFoois a function object, so you can directly use theFoowithout having to call it to run (should understandFooand theFoo() differences between (). We can putFooA parameter is passed to the function, or the function object is assigned to another variable (named,other_function = foo). Because functions are treated as a class of objects, many functions are the same as normal objects.
In the second part, we'll talk about lower-level code objects.
Reference:
http://akaptur.com/blog/2013/11/15/introduction-to-the-python-interpreter/
Cai Junsheng qq:9073204 Shenzhen
A discussion of the Python interpreter: The first part of the function object