**Is Python an interpreted language? **
Python is an interpreted language, and I just believed it until I found out the existence of *.pyc files.
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 sentence, then we will clarify this issue in the article and clarify some basic concepts. Python is not entirely an interpreted language. It is compiled. First, the source py file is compiled into pyc or pyo, and then executed by the python virtual machine. Compared with the py file, the compilation into pyc and pyo is essentially nothing like py. It’s a big difference. It’s just that the loading speed of this module has been improved, but the execution speed of the code has not been improved. Under normal circumstances, it is not necessary to actively compile the pyc file. The document says that as long as the import model is called, the model.py will be compiled into pyc first. Then load
Interpreted and compiled languages
Computers cannot recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in the process of converting the high-level language into a machine language that the computer can understand. This process is divided into two categories, the first is compilation, and the second is interpretation.
Before the program is executed, the compiled language will perform a compilation process on the program through the compiler, and convert the program into machine language. There is no need for translation at runtime, and just execute it directly. The most typical example is the C language.
Interpreted languages do not have this compilation process. Instead, when the program is running, the interpreter interprets the program line by line, and then runs it directly. The most typical example is Ruby.
Through the above examples, we can summarize the advantages and disadvantages of interpreted languages and compiled languages, because compiled languages have already "translated" the program before the program is run, so "translation" is omitted at runtime. "Process, so the efficiency is relatively high. But we can't generalize, some interpreted languages can also optimize the entire program when translating the program through the optimization of the interpreter, so that the efficiency is close to the compiled language, but can not exceed the compiled language.
In addition, with the rise of languages based on virtual machines such as Java, we cannot divide languages purely into interpreted and compiled languages.
Using Java as an example, Java is first compiled into a bytecode file by a compiler, and then interpreted into a machine file by an interpreter at runtime. So we say that Java is a language that is compiled and then interpreted.
What exactly is Python
In fact, Python, like Java/C#, is also a language based on a virtual machine. Let's first understand the running process of Python programs on the surface.
When we entered python hello.py on the command line, we actually activated the Python "interpreter", telling the "interpreter": you are about to start working. But before the "interpretation", the first work performed is the same as Java, which is compilation.
Briefly describe the running process of Python
Before talking about this issue, let's talk about two concepts, PyCodeObject and pyc file.
The pyc we saw on the hard disk is naturally needless to say, but in fact, PyCodeObject is actually the result of the Python compiler. We can simply know it first, and continue to look down.
When the python program is running, the result of the compilation is saved in the PyCodeObject located in the memory. When the Python program runs, the Python interpreter writes the PyCodeObject back to the pyc file.
When the python program is run for the second time, first the program will look for the pyc file in the hard disk. If found, it will first judge the last modification time of the .pyc file and the .py file. If the modification time of the .pyc file is later than. py file, indicating that the source code in the .py file has not been modified, load it directly, otherwise repeat the above process.
So we should locate PyCodeObject and pyc files in this way. We say that pyc files are actually a persistent storage method of PyCodeObject.
Knowledge point supplement:
In fact, understanding the execution process of Python programs is of little significance to most programmers, including Python programmers. What really makes sense is what we can learn from the practice of Python interpreters. I think there is such a thing. A few points:
A. In fact, whether Python is saved as a pyc file is the same as when we designed the cache system. We can think carefully about what is worth throwing in the cache and what is not worth throwing in the cache.
B. When running a time-consuming Python script, how can we slightly squeeze the running time of some programs, that is, separate the module from the main module. (Although this is often not a bottleneck)
C. When designing a software system, should the reusable and non-reusable things be treated separately? This is an important part of the software design principles.
D. When designing a cache system (or other systems), how do we avoid program expiration? In fact, the Python interpreter also provides us with a particularly common and effective solution.
This is the end of this article on whether python is an interpreted language. For more related python is an interpreted language, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !
Recommended Posts