Home > Article > Backend Development > What is the reason why Python does not have a main function?
These languages are all compiled languages, and the code needs to be compiled into an executable binary file. In order for the operating system/bootloader to find the beginning of the program, such a function needs to be defined.
In short, a crucial beginning needs to be defined in a large amount of executable code.
It is not difficult to see that for these languages, the main function is an indispensable component.
However, when we turn our attention to Python, we will find that the situation is quite different.
Python is an interpreted language, a scripting language. The running process is performed from top to bottom, line by line, which means that its starting point is known.
Each .py file is an executable file that can be used as the entry file for the entire program, which means that the entry of the program is very flexible and does not need to follow any conventions.
Sometimes there is no need to specify the entry file when running a Python project (the command line is more common, such as "python -m http.server 8000"), possibly because there is main. py file, executed as a "file" in the software package.
In short, Python as a scripting language is different from a compiled language. Whether it is a single module (i.e. .py file), or a software package composed of multiple modules, Python can choose a flexible execution method, which is completely unlike other languages that must define entry points.
In other words, Python does not require programmers to define a unified entry in syntax (whether it is a function, class or something else).
Some students may be confused because they often see or write the following code:
# main file def main(): …… if __name__ == '__main__': main()
Isn’t this the main function of Python? I believe many people think so!
No, not really.
Except that the function name is "main", this code has nothing to do with the main function we introduced earlier. This function is neither necessary nor can it determine the execution order of the program. Even if there is no main function like the above, there will not be any syntax problems.
The main reason why people want to write a main function is actually to emphasize that this is a main function and hope to artificially set it as the first function to be executed.
They may think that the function with this name is easier to remember.
The reason why they want to write name ==‘main’ may be because they want to indicate that main() only executes the current script directly. Run it only if it is imported into other modules, but not when it is imported into other modules.
However, I personally do not recommend this way of writing.
To give a simple example, suppose there are only a few dozen lines of code, or a script file implements a simple function (a crawler, or drawing a turtle, etc.), but all in accordance with Written in the previous way.
It is not recommended to write if name == 'main’, because:
First of all, if there is only one file, then this file cannot May be exported.
Secondly, if there are multiple files, it is strongly recommended not to write this statement in the entry file (main.py). In theory, its contents should not be exported for use by other modules, since it is the starting point.
Finally, in the case of multiple files, it is not recommended to write this statement in a non-entry file, because the most that this statement can do is write some tests. code. Even so, the test code should be written separately to a dedicated directory or file.
I feel sick every time I see this clumsy code. Why write such an if statement? You shouldn't wrap this code into a function at all!
The above is the detailed content of What is the reason why Python does not have a main function?. For more information, please follow other related articles on the PHP Chinese website!