Two days ago did a netease multiple choice question:

Problem Description:

Python is written in C. Based on the namespace feature, the following code is compiled by the Python compiler to produce () PyCodeObjects.

class A:
    pass
def Fun():
    pass
a = A()
Fun()
Copy the code

. What is a PyCodeObject?

It’s not a very long story

Here are some learning details. For example, I was looking for the cause of problem A, but at this time, the three problems of B.C.D became more and more confused. Finally, I gave up. Trying to solve the problem thoroughly will lead to progress.

Okay BB so much, WHAT I want to say is to do things thoroughly, because it will bring wonderful emptiness. It is not a dirty joke, it is cui Jian’s song (Blue Bone)…

Interpretive language from scratch? Compiled languages?

Compiled and interpreted languages: For example, if you create a book, you want to find a translator to translate it into English history so that you can become an Internet sensation. So your secretary has two interpreters, translate and print:

1. Compiled translation (language) : Translate from the beginning all the way to the final punctuation mark of your work, then start printing, and the 1 is ready to rest

2. Interpretive translation (language) : Just like your mother telling you a story at the bedside, translate sentence by sentence or paragraph by paragraph, sentence by sentence, and then print sentence by paragraph, 2 always accompany you

Analyze the advantages and disadvantages of the two methods:

1 high operation efficiency, a molding elevator house bag check-in, according to your stomach, is once the translation is finished, the execution efficiency is very high.

For example, when you ask 1 to translate for you, you find that I have a wrong word, but if 1 is finished, he must wait for his translation, and then correct and ask him to translate again. What if it’s 2, paragraph by paragraph, line by line, just re-translate that line?

Summary:

To translate your code into a language that computers can understand, there are two plans

1. Compilation, like C, once compiled into machine language, directly to the machine to execute, so the execution efficiency is very high

2. Explain, omit the compilation process, explain line by line directly run, like Ruby

WTF, haven’t said Python for so long, so what is Python?

Python, like Java (virtual machine based language), is compiled and then explained. It is easy to say that Python is compiled. If you open your Pycharm and write a.py file successfully, you will find an extra.pyc file in your folder. To run the.py file in terminal, you have to type Python xxx.py, which is the word that wakes up the interpreter to sleep

So much for language differences, let’s consider Python

Pyc and PyCodeObject

First, I will do a small test. I have written the following code on the native machine. After running successfully, I want to see if there is a PYC file under my file

I wipe? Why doesn’t WTF?

Now let’s create a new.py file and import the say method from the memo file

Ah? Why are PYC files coming out again?

The reason: Pycodeobjects are actually compiled in memory by the Python compiler, and pyC files can be thought of as persistent PyCodeObjects on disk. The reason pyC files are not produced is because the Python interpreter decides that it is worthwhile to reuse code blocks in memory to see if they are in PyCodeObject before pyC execution. If they are, there is no need to read the local pyC file on disk. The process is as follows

So to go back to the original question, it seems like a little bit of a roundabout

To answer this question, let’s first explain the concept of Code Block:

When compiling Python source Code, the Python compiler creates a PyCodeObject corresponding to a Code Block in the Code.

How do I determine how much Code counts as a Code Block?

Python rules for determining a Code Block: When entering a new namespace or scope, a new Code Block is entered.

That is, a namespace corresponds to a Code Block, which corresponds to a PyCodeObject.

For now, think of namespaces as the context of symbols, the mapping of names to objects.

In Python, classes, functions, and Modules all correspond to a separate namespace and therefore to a PyCodeObject.

class A:
    pass
def Fun():
    pass
a = A()
Fun()
Copy the code

After compiling the source code above, the Python compiler creates three PyCodeObjects: the first for the code Block corresponding to the whole.py file, the second for the code Block corresponding to Class A, and the third for the code Block corresponding to f.

Correct answer check is three, small hand a shake three points hand, That’s all THX ~