Talk about the Python program's execution process.

Source: Internet
Author: User

1. Is python an interpreted language?

The first thing I heard about Python when I was a beginner python was that Python was an explanatory language, and I kept believing until I found the *.pyc file. If it is an interpreted language, what is the generated *.pyc file? c should be the abbreviation of compiled!

In order to prevent other people who learn python from being misunderstood by this remark, we will clarify this issue in the text and make some basic concepts clear.

2. Explanatory and compiled languages

Computers are not able to recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in the process of translating high-level languages into machine languages that computers can read. This process is divided into two categories, the first of which is compilation, and the second is interpretation.

A compiled language before a program executes, the program executes a compilation process through the compiler, transforming the program into machine language. The runtime does not need to be translated and executes directly. The most typical example is the C language.

The explanatory language does not have this process of compiling, but rather, when the program is running, it interprets the program line by row, then runs directly, and the most typical example is Ruby.

Through the above example, we can summarize the advantages and disadvantages of the explanatory language and the compiled language, because the compiler language before the program has already made a "translation" of the program, so at run time there is less "translation" process, so the efficiency is higher. But we also can't generalize, some interpretive languages can also be optimized by the interpreter to optimize the whole program when translating the program, thus more efficiently than the compiled language.

In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide the language into two types-----explanatory and compiled.

In Java, for example, Java is first compiled into a bytecode file by a compiler and then interpreted as a machine file by the interpreter at run time. So we say that Java is a language that is compiled and interpreted first.

Change to c#,c# First, the C # file is compiled into an IL file by the compiler, and then the Il file is compiled into a machine file through the CLR. So we say that C # is a purely compiled language, but C # is a language that needs to be compiled two of times. The same can be applied to the basis of equivalence. Other languages on the net platform.

3. What exactly is Python?

In fact, Python, like java/c#, is also a virtual machine-based language, let's start with a simple look at the Python program's running process.

When we enter Python hello.py on the command line, we actually activate the Python interpreter and tell the interpreter: You're going to start working. But before the "explain", the first thing that actually executes is the same as Java, which is compiled.

Students familiar with Java can consider how we execute a Java program on the command line:

Javac Hello.java

Java Hello

Just when we were using an IDE like Eclipse, we fused these two parts into a piece. In fact, Python is also the same, when we execute Python hello.py, he also executes such a process, so we should describe the Python,python is a first compiled after the interpretation of the language.

4. Brief description of Python's running process

Before we say this question, let's start with two concepts, pycodeobject and PYC files.

The PYC we see on the hard drive naturally doesn't have to say much, and pycodeobject is actually the result of a Python compiler actually compiling it. Let's just get to the bottom of it and keep looking down.

When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

When the Python program runs for the second time, the program will first look for the PYc file on the hard disk, and if it is found, load it directly or repeat the process.

So we should be able to locate Pycodeobject and pyc files, we say that PYc file is actually a kind of persistent saving way of pycodeobject.

5. Run a python program

Let's write a program that actually runs:

The program itself has no meaning. We continue to look at:


However, we do not see the PYc file in the program, still is test.py alone to stay there!

So let's change the way we put the Print_str method into another Python module:



Then run the program:


This time PYc file appeared, in fact, seriously think about it is not difficult to get the reason, we consider the actual business situation.

6. The purpose of PYC is to reuse

Recalling that the second paragraph of this article explains the pros and cons of compiled and interpreted languages, I say the advantage of a compiled language is that we can use a file that has been "translated" without explaining it while the program is running. That is, the biggest advantage of compiling a py file into a PYc file is that we do not need to re-interpret the module when we run the program.

So, what we need to compile into PYC files is those that can be reused, which is the same thing we do when we design software classes. So the Python interpreter thinks: Only the module that import comes in, is the module that needs to be reused.

This time maybe someone will say, wrong ah! Your problem has not been explained, Ah, my test.py not need to run, although not a module, but I can save time every time Ah!

OK, let's start with the actual situation and think about when we might be able to run the Python xxx.py file:

A. When testing is performed.

B. When a Web process is started.

C. Execute a program script.

One way or the other, we don't have to talk about the first situation, even if all the files are not PYC files are irrelevant.

In the second case, let's imagine a webpy program that we usually do:


Or a person who:


The program then looks like a daemon that keeps tabs on port 8181/9002, and once it's broken, it's only possible that the program is killed, or otherwise, what you need to restore is to restart the entire Web service. So since we've been watching, it's enough to keep the pycodeobject in memory, and it's completely unnecessary to persist to the hard disk.

In the last case, execute a program script, the main entrance of a program is very similar to the controller in the Web program, that is, he should be responsible for the scheduling between the model, not including any of the main logic, such as I http://www.cnblogs.com/ As mentioned in kym/archive/2010/07/19/1780407.html, controller should be a facade, no detail logic, just to turn the parameters, then if the students can know, in an algorithm script, The most easy to change is the parameters of the algorithm, then this time to persist into the PYc file is somewhat superfluous.

So we can understand the intent of the Python interpreter so that the Python interpreter only persists the modules that we might reuse into PYC files.

7. Expiry time of PYC

After saying the PYc file, one might think that each time the Python interpreter has persisted the module to the PYc file, then when my module has changed, is it not always to manually remove the previous PYc file?

Of course, Python's designers don't make such stupid mistakes. This process depends on how Pycodeobject is written to the PYc file.

Let's take a look at the source code of the import process:


This code is long, we only look at the code I marked, in fact, when he wrote the PYc file, write a long variable, the content of the variable is the file's last modified date, similarly, we look at the download into the PYC code:



Without looking at the code, we can clearly see the principle, in fact, each time before loading will check the py file and PYc file save the last modified date, if inconsistent, regenerate a copy of the PYc file.

8. Written in the last

In fact, understanding the execution of a Python program is of little significance to most programmers, including Python programmers, so what really makes sense is what we can learn from the Python interpreter's approach, and I think there are a few things:

A. In fact, whether Python is saved as PYc file is the same as when we design the cache system, we can think about what is worth throwing in the cache, what is not worth in the cache.

B. When running a time-consuming Python script, how can we squeeze some of the program's run time, which is to separate the module from the main module. (although this is often not a bottleneck)

C. When designing a software system, reuse and non-reusable things should also be treated separately, which is an important part of the software design principles.

D. In designing the caching system (or other systems), how do we avoid the expiration of the program, in fact, the Python interpreter also provides us with a particularly common and effective solution.

Turn http://www.cnblogs.com/kym/archive/2012/05/14/2498728.html

Talk about the Python program's execution process.

Related Article

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.