.pyc file of python

What is .pyc?

Computers cannot recognize high-level languages, so when we run a high-level language program, we need a "translator" to convert 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, a compiled language will first perform a compilation process on the program through the compiler to convert the program into machine language. There is no need for translation at runtime, and direct execution is fine. The most typical example is the C language.

Interpreted languages ​​do not have this compilation process, but when the program is running, the program is interpreted line by line through the interpreter, and then runs directly. The most typical example is Ruby.

Because the compiled language has already "translated" the program before the program is run, the "translation" process is omitted at runtime, so the efficiency is relatively high. But we can't generalize, some interpreted languages ​​can also optimize the process through the optimization of the interpreter.

When the program is translated, the entire program is optimized, so that it is more efficient than compiled languages.

With the rise of virtual machine-based languages ​​such as Java, we cannot divide languages ​​purely into interpreted and compiled languages. Java is first compiled into bytecode files by a compiler, and then interpreted into machine files by an interpreter at runtime. therefore we

It is said that Java is a language that is compiled and then interpreted.

In fact, Python, like Java/C#, is also a virtual machine-based language.

When we enter python hello.py on the command line, we actually activate the Python "interpreter" and tell the "interpreter": you are about to start working. But before the "interpretation", the first work performed is actually compiling, the same as Java.

When we execute python hello.py, he also executes the process of java, so we should describe Python like this. Python is a language that is compiled and then interpreted.

 

The running process of Python

Two concepts, PyCodeObject and pyc files.

The pyc we see on the hard disk naturally does not need to say much, but in fact PyCodeObject is the result of the actual compilation of the Python compiler.

When the python program runs, the compiled result is stored 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 runs for the second time, first the program will look for the pyc file in the hard disk. If it is found, it will be loaded directly, otherwise the above process will be repeated.

So we should locate PyCodeObject and pyc files in this way. We say that pyc files are actually a persistent way of saving PyCodeObject.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325228309&siteId=291194637