Python, article 3: What is. pyc ?, Python. pyc

Source: Internet
Author: User

Python, article 3: What is. pyc ?, Python. pyc

What is. pyc?

1. Is Python an interpreted language?

When I was a beginner in Python, the first sentence I heard about Python was that Python was an explanatory language, so I kept believing until I found the existence of the *. pyc file. If it is an interpreted language, what is the *. pyc file generated? C should be the abbreviation of compiled!

To prevent other Python learners from being misunderstood, let's clarify this question and clarify some basic concepts.

 

2. interpreted and compiled languages

Computers cannot recognize advanced languages, so when we run an advanced language program, A "translator" is needed to convert advanced languages into machine languages that computers can understand. This process is divided into two types: Compilation and interpretation.

Before executing a program, the compiled language will first execute a compilation process on the program through the compiler to convert the program into a machine language. You do not need to translate it during the running, but simply execute it. The most typical example is the C language.

This compilation process does not exist in the interpreted language. Instead, the interpreter is used to explain the program line by line and then run it directly. The most typical example is Ruby.

Through the above example, we can summarize the advantages and disadvantages of interpreted and compiled languages, because the compiled language has already translated the program before it runs the program ", therefore, the process of "Translation" is reduced during operation, so the efficiency is relatively high. However, we cannot generalize either. Some interpreted languages can also optimize the entire program during Program Translation through interpreter optimization, thus surpassing the compiled language in efficiency.

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

Java is used as an example. Java is first compiled into a bytecode file through the compiler, and then interpreted as a machine file through the interpreter at runtime. Therefore, Java is a language that is compiled first and then interpreted.

 

3. What is Python?

In fact, Python is also a virtual machine-based language like Java/C #. Let's take a look at the running process of the Python program.

When we input python hello. py in the command line, it actually activates the "interpreter" of Python and tells the "interpreter" that you are about to start working. However, before "explanation", the first task to be executed is compiling Like Java.

If you are familiar with Java, you can think about how to execute a Java program in the command line:

Javac hello. java

Java hello

 

It's just that when we use an IDE such as Eclipse, we integrate these two parts into one. The same is true for Python. When we execute python hello. py, he also executes this process, so we should describe Python in this way, Python is a language that is compiled first and then interpreted.

4. Briefly describe the Python running process

Before talking about this, let's talk about 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 actually compiled by the Python compiler. We can simply know it and continue to look down.

When the python program runs, the compilation result is saved in the PyCodeObject 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, the program first looks for the pyc file in the hard disk. if it finds the file, it loads it directly; otherwise, the above process will be repeated.

Therefore, we should locate the PyCodeObject and pyc files in this way. We say that the pyc file is actually a persistent storage method of PyCodeObject.

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.