Python Interpreter: Part 1 function object, python Interpreter

Source: Internet
Author: User

Python Interpreter: Part 1 function object, python Interpreter
Python Interpreter: Part 1 function object

 

In the last three months, Ned Batchelder and I have spent a lot of time developing byterun. This project byterun (https://github.com/nedbat/byterun) is an interpreter for python bytecode developed using python. In the byterun process of the development interpreter, I learned a lot and brought a lot of fun. In this series, I will take you to experience the fun of development and the pleasure of using byterun. However, before experiencing a happy experience, you must first understand some basic knowledge, just like warming up before doing sports: have a general understanding of the internal implementation of python, so that we can know what the interpreter is, what it is used for, and what it cannot do.

 

This article assumes that you know python, which is the same as I did three months ago. I don't know anything except this, let alone the internal implementation of python.

 

It is worth noting that the main focus of this article is on the basis of python2.7. In fact, the interpreter of python3.0 is not much different, similar to the previous version. The main difference is that some syntaxes and names are not the same. The underlying content can be applied to Python.

 

How does python work?

We will start from the advanced interactive interface to understand how python works internally. When you input a line of code in the python interactive interpreter and run it, what is the interpreter doing?

 

~ $ 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 enter the line of code and press the Enter key, the python interpreter has four things: lexical analysis, syntax analysis, compilation, and execution of bytecode. The process of lexical analysis is to analyze the entered code and generate one word (token ). The syntax analysis is to analyze these words, and then generate a structure suitable for expressing the relationships between words based on their relationships (here is the AST abstract syntax tree ). During the compilation and generation of code, the AST structure is used to generate one or more code objects, and the code objects have become bytecode and data. Finally, the explain execution stage runs the bytecode in each code object.

 

In today's article, I am not going to discuss lexical analysis, syntax analysis, or compilation and generation of code, because I don't understand these stages and can't express them in my language. Instead, I assume that all of these are running correctly and can generate appropriate python code objects for the interpreter to run.

 

Before discussing code objects, we should first introduce some background knowledge. In this series of articles, we will discuss function objects, code objects, and bytecode. These three are completely different, and then we will discuss function objects first. Although you can understand the internal functions of the interpreter without understanding function objects, I want to emphasize that function objects and code are two different things, and function objects are more special.

 

Function object

You may often hear about function objects because people often talk about them, such as "function objects are the most important class objects" or "python code is mainly composed of function objects ". Let's see how the function object looks like:

>>> Def foo ():

... X = 3

... Return x +

...

>>> Foo

<Function foo at 0x0000ef7aa0>

 

"Function objects are the most important type of objects" means that a function is an object, not just a list object or a MyObject object. Because foo is a function object, you can directly use foo without calling it for running (the difference between foo AND foo () should be understood ). We can pass foo as a parameter to the function, or assign the function object to another variable (named, other_function = foo ). Since a function is treated as a Class Object, many functions are the same as common objects.

 

In the second part, we will discuss lower-level code objects.

 

Refer:

Http://akaptur.com/blog/2013/11/15/introduction-to-the-python-interpreter/

 

 

Cai junsheng QQ: 9073204 Shenzhen

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.