The first part:
To explore this problem, or because of the error encountered during programming:
Hint Tcplink is not defined, Tcplink is a function I wrote to the supervisor to hear the TCP connection request to allocate a new thread, but write it down, just like this:
If it's in C + +, it's easy to solve the problem. At the beginning of the file, plus the declaration of the function is OK, so not only convenient, but also to maximize the appearance (fog). But the problem is, Python does not seem to have a declaration and definition of that!
Is there anything in the end? This has to be seen from the running mechanism of the Python script.
In C + +, the declaration is to tell the compiler that my program will have this symbol, the compiler will record the declaration content, record the entry at the definition, allocate memory. In other words, because C + + is a compiled language, this allows the full program to be scanned for cross-text field connections.
However, Python is not possible, Python is an interpreted language, although our own script is a complete file, but in the execution of the Python interpreter, it is still equivalent to the script file in a row of input into the interpreter and execute. This results in the execution of the current statement to invoke Tcplink, the interpreter immediately in the previous input to find the definition of tcplink and execution, if unable to execute the error. Because of this mechanism, it leads directly to the inability to put a declarative in front of the definition in the same way as in C + + +.
Part II:
Some people may ask, pull down, I write this when programming, FUNCB in front of FUNCC, FUNCB inside will execute FUNCC, why run when what error will not be reported?
This is even more, let's start by putting this little piece into the python command-line interactive mode to see how the results are:
Yes, and there's still nothing wrong. Then, according to the above, the Python interpreter is in a row to explain a row, if unable to explain the error will be immediately, why the interpreter read into the FUNCC, the user did not make the definition of FUNCC, the interpreter did not error it?
In fact, the "Python interpreter is a line to explain a line" this is not very rigorous statement. The attentive reader can find that when the Python interpreter enters Def FUNCB (): And returns to the,>>>, it becomes ... only after the FUNCB user definition is complete and confirmed, will it return to >>>. In general, the user is entering a line of carriage return,>>> does not change, and output should have results. So what does that mean? Two-point question:
1. When defining FUNCB (), the user's carriage return does not allow the input statement to execute.
2. When the definition is complete FUNCB () sends a carriage return acknowledgement to the interpreter, the interpreter does not execute the contents of the previous definition . (Because if you do, you will get an error, just like this)
The new problem comes up again, so what did the interpreter do? The answer is: "Define FUNCB ()" is executed. In this way, the second part of the question, I think we should have an answer in mind. Take a picture of the program execution process that starts with the second part, just like this:
In short, if you are currently simply executing the "define XXX" statement, the interpreter does not care what you specifically define , and the interpreter does not perform the content at this time. So because the previous FUNCC () is defined within the FUNCB (), Nature does not execute, and there is no question whether there is FUNCC () at this time. But! If I call FUNCB at this point, the interpreter will instead execute the specific contents of the FUNCB, and if FUNCC () is not defined, it will report the error of not defined! Let's check it out:
It seems to be a good say. And the second part of the code at the beginning of the FUNCC is actually executed, has obtained a complete definition, it will not report the error of not defined pull ~
Part III:
So, is there any way to make my Python program look more neat and tidy--so that there are too many functions in the so-called "main program" file that seem cluttered?
In fact, import is a very good way, a little familiar with the Python people understand that import can refer to other parts of the Py module, can also be used in the form of the Import function from module, the specified function within the module, class.
Even!!
For a person who comes from C + +, it's silly. The import of Python is equivalent to "import things intact into the import of a whole lump", so because it is within the function definition import, so import things only within the function can be used ~ but C++/C's separate # The include preprocessing directives are not.
Wait a minute. is not missing something important ...
Yes, if import imports this file directly, is it OK? If so, wouldn't that be the case before, plus:
From this file import funca/b/c, you can implement a similar function declaration of C + + functions? If it succeeds, wouldn't it be ... How fragrant?
Let's try it out:
It seems that the true fragrance failed. So why is this method not working? Let's review the second part of the middle of the I draw the flowchart, because the Python interpreter is come in a sentence to execute, so the content here is: Import daliywork in the FUNCB. When executing this sentence, the FUNCB is not defined, so the import fails.
Part IV:
The last question, if you import the entire file itself, can you achieve this result? (Just as the interpreter does not explore the exact definition when you define XXX, this only performs the import of the entire file, does the interpreter care about the specifics of the entire file?). )
Let's review the third part of the import a very intuitive explanation:
Well, with this explanation, I think most people have an answer in their hearts. The test code is as follows, and we take a look at the results directly:
The exception stack first prompts Funca not defined, pointing to Daliywork line sixth, but this error is caused by the third line of import of Daliywork. This is because the import of the entire file, the equivalent of the other than the import of the sentence, instead of the import itself to the place, and then execute the code, at this time, in the introduction of the part, executed to Funca, found or not defined, At this time, the Funca did not make a definition error. Note that the error is reported in the Funca is not defined, is not reported in the execution file Funca not defined (because at this time in the source file Line3 Import This sentence has thrown an exception, the program has been parked here!). ), but the sixth line in the import daliywork error . This is very important and can be explained by the following illustrations (PS: Drawing is good):
Although the introduction of the content, including all the definition of content, but because the introduction of the file will also happen , so we still do not realize the effect we want ...
It seems that because of Python's special interpretation execution mechanism, there is no way to advance only the declaration of a function. In the future when writing code, or obediently either the definition of the function is put into other files through the Import module mode, the call through the form of module.function (), or obediently put in the code to execute the function in front of it ...
Untitled
Declaration definition to be separated, Python interpretation line not?
The ever-changing difficult to imitate, around a circle of empty sigh.
Is there a declaration and definition in python that says "little thought"?