This blog is divided into four parts, each of which can be seen as a separate, but closely related, problem.


Part I:

I explored this problem because I encountered this error in programming:

Tcplink is a function that assigns new threads to TCP connections that are being listened on, like this:

If it is C++, it is very easy to solve this problem. Add the function’s declarative form at the beginning of the file, which is both convenient and aesthetically pleasing as much as possible. There is no such thing as declaration and definition in Python.

Is there one or not? This depends on how Python scripts work.

In C++, the declaration tells the compiler that I’m going to have this symbol in my program, and the compiler records the declaration, records the entry at the definition, and allocates memory. In other words, since C++ is a compiled language, it is possible to scan the entire program and make connections across text fields.

Python, on the other hand, is an interpreted language, and even though our script is a complete file, when we write it to the Python interpreter, we still type the script file line by line into the interpreter and execute it. As a result, if the current statement being executed calls tcplink, the interpreter will immediately look for the definition of tcplink in the previous input and execute it, or report an error if it cannot execute. Because of this mechanism, it is not possible to put a declaration first and then put the definition elsewhere, as C/C++ does.


Part II:

Some people might say, well, I wrote funcb before funcc, and funcb will perform funcc inside funcb, so why don’t we get anything wrong when funcb is run?

Let’s put this snippet in Python command line interaction mode and see what happens:

Yeah, and again, nothing went wrong. Funcc: Funcc: funcc: funcc: funcc: funcc: funcc: funcc: Funcc: funcc: funcc: funcc

In fact, the statement that the Python interpreter explains line by line is not a very serious one. Careful readers will notice that when def funcb () is typed in the Python interpreter and entered, >>> becomes… Only after funcb has been user-defined and confirmed will it go back to >>>. Typically, the user enters a line return, >>> does not change, and outputs the expected result. So what does that tell us? This points to two things:

1. When funcb() is defined, the user’s carriage return does not allow the entered statement to execute.

2. When a function () is finished, the interpreter does not execute the function. (Because if executed, an error will be reported, as follows)

New questions arise, so what exactly did the interpreter perform before? The answer is “Define funcb ()”. So I think you have the answer to the question at the beginning of Part two. Draw a picture of the execution of the program at the beginning of Part 2, like this:

In short, if you currently execute a “define XXX” statement, the interpreter doesn’t care about what you specifically define, and the interpreter doesn’t execute what comes in. Therefore, since the former funcc() is the content of the definition of funcb(), it is not implemented naturally, and it does not matter whether funcc() exists at this time. But!!! If I call funcb at this point, the interpreter will execute funcb instead, and if funcc() has not yet been defined, it will say not defined! Let’s verify this:

That seems to be a good point. The code at the beginning of the second part, when funcc is actually executed, has been fully defined, so it will not report not defined


Part III:

So, is there a way to make my Python programs look cleaner and less cluttered by having too many functions in the so-called “main program” file?

If you are familiar with Python, import can refer to modules such as py from other places. You can also use from Module import function to import a specific function or class from a specific module.

Even!!!!!

For a guy from C++, it’s stupid. Python’s import is equivalent to “stuffing an import into an import”, so since it is imported from within a function definition, it is only possible to use an import within that function, whereas C++/C’s separate #include preprocessor directive does not.

And so on. Are you missing something important…

Yes, is it possible if import is directly imported into this file? If possible, wouldn’t it be better to add:

Import funca/b/c from import funca/b/c If it works, wouldn’t it be… Smells good?

Let’s try:

Looks like, really sweet failure. So why doesn’t this approach work? Let’s review the flow chart I drew in the middle of Part 2. Since the Python interpreter executes each sentence, it says: Import Funcb from Daliywork. Funcb is not defined when this sentence is executed, and the import fails. But it is important to note that the “import file itself” works! See the following example:

Does the imported Daliywork in the imported daliywork generate execution? If so, where is the execution displayed?


Part IV:

Finally, can this be achieved by importing the entire file itself? (Does the interpreter care about the contents of the entire file, just as the interpreter does not explore the contents of the specific definition of XXX?)

Let’s review a straightforward (but not rigorous) explanation of import from Part 3:

Well, with that explanation, I think most people already have the answer in their minds. Test code is as follows, let’s directly execute to see the result:

The exception stack first tells funca that it is undefined and points to line 6 of Daliywork, but this error is again caused by the third line import of Daliywork. This is because the import of the entire file is equivalent to replacing everything except the import sentence with the import itself, and then executing the code. In this case, funca in the introduced part is still undefined, and then reporting the undefined error of funca. Funca is undefined (‘ funca ‘is undefined); funca is undefined (‘ funca’ is undefined); , but the sixth line error in the import daliywork. This is very important and can be illustrated by the following illustration (PS: It’s easy to draw) :

Although the imported content includes all the definition content, the imported file will also be executed, so we still can’t achieve the desired effect…

It seems that due to Python’s special interpretive execution mechanism, there is no way to simply declare functions ahead of time. When writing code in the future, you should either import the functions defined in other files as import Modules and call them as module.function(); Or put it in front of the code that executes the function…


Finally, a little poem.


The object

Declare the definition to separate, Python explanation ok?

Kaleidoscopic difficult to simulate, around a circle empty sigh.