preamble
As we all know, there is no such thing as a main function in Python, but there are often articles on the web that refer to "Python's main function" and "Recommendations for writing a main function".
Actually, maybe they were trying to mimic the real main function, but many people have been misled (or misinterpreted) and then written very clumsy code.
Before we begin our discussion, let's answer the following two questions:
- What does the term "main function" mean?
- Why do some programming languages have to write main functions?
Some programming languages use the main function as the entry point for the execution of a program, such as C/C++, C#, Java, Go, Rust, etc. This function has a specific meaning:
- The main function name is required, which means there must be a main function.
- There can be at most one main function, which means that the entry point to the program is unique.
- There are specific requirements for grammatical formatting and a relatively fixed form of writing.
Why must the main function be forced as an entry point?
These languages are compiled languages and need to compile the code into an executable binary file. In order for the operating system/bootloader to find the beginning of the program, a function like this needs to be defined.
In short, a crucial beginning needs to be defined in a large amount of executable code.
Not surprisingly, the main function is an integral part for these languages.
However, when we turn our attention to Python, we realize that the situation is very different.
- Python is an interpreted language, i.e. a scripting language. The runtime process is top-to-bottom, line by line, which means that its starting point is known.
- Each .py file is an executable that can be used as an entry file for the entire program, meaning that the entry to the program is flexible and does not have to follow any conventions.
- Sometimes a Python project can be run without having to specify an entry file (the command line is more common, e.g., "python -m 8000"), probably because there are files in the project that are executed as "files" in the package.
In short, Python as a scripting language is different from a compiled language. Whether it is a single module (i.e., a .py file) or a package consisting of multiple modules, Python has the option of a flexible method of execution, which is not at all like other languages where you have to define entries.
In other words, Python doesn't need to dictate that programmers must syntactically define a uniform entry point (whether it's 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 Python's main function? I'm sure many people think so!
No, not really.
This code has nothing to do with the main function we described earlier, except that the name of the function is "main", which is neither required nor determines the order of execution of the program. Even without the main function, there would be no syntax problems.
The main reason people want to write a main function is really to emphasize the fact that it's a main function, and they want to artificially set it up to be the first function to execute.
They may think that functions with this name are easier to remember.
The reason they are writingname ==‘main', probably because it wants to indicate that main() should only be run when directly executing the current script, and not when importing it into other modules.
However, I personally don't recommend this writing style.
As a simple example, suppose there are only a few dozen lines of code, or a script file that implements a simple function (a crawler, or drawing a turtle, etc.) but is written in the same way as before.
Not recommended ifname == 'main' is written as:
- First of all, if there is only one file then that file cannot be exported.
- Secondly, it is highly recommended not to write this statement in the entry file () if there are multiple files. Theoretically, its contents should not be exported for use by other modules, as it is the starting point.
- Finally, in the case of multiple files, it is also not recommended to write this statement in a non-entry file, as the most this statement can do is write some test code. Even then, the test code should be written separately to a dedicated directory or file.
Every time I see this clunky code, I get sick to my stomach. Why would you write an if statement like that? You shouldn't be wrapping this code as a function at all!
summarize
- Break the inertia and write real code. main function is the only entry point for some languages but should not be used in Python. You should understand the characteristics of a scripting language and learn a simple and elegant style.
- You can use, instead of writing, the main function. Since the unit of execution of a Python program is a script file, not a function or class, it is recommended that you name the entry file and decide on the internal functions as needed.
- will be used as the entry file. This file can be used directly in conjunction with the "-m" parameter on the command line.
to this article on why Python does not have a main function of the article is introduced to this, more related Python does not have a main function of the content of the search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!