There is no such thing as main in Python, of course, but there are often articles on the Internet that mention “Python’s main function”, “recommend writing main”…

Some of you know it, and your intent may be to copy the original main function, but many of you are clearly misguided (or misunderstood) and write very cumbersome code.

Why does Python have a main function?

Before we get started, we need to answer two questions: What is the so-called “main” function? Why do some programming languages force a main function?

Some programming languages, such as C/C++, C#, Java, Go, and Rust, use the main function as the entry point for the program execution. These functions have specific meanings:

  • The main function name is mandatory, that is, it must have a main function
  • There can be at most one main function, which means that the entry to the program is unique
  • Syntax format has certain requirements, with a relatively fixed template

Why force a main entry function?

These languages are compiled languages that need to compile code into executable binaries, so this function is required in order for the operating system/initiator to find the starting point of the program. In short, in a large number of code, you need to define a significant beginning that can be used for execution.

As you can see, the main function is an important and indispensable part of those languages.

Look at Python, however, and it’s a different story.

  • Python is an interpreted, or scripting, language that runs line by line, from the top down, meaning that its starting point is known
  • Each.py file is an executable file that can be used as an entry file for the entire program, meaning that the entry of the program is flexible and there are no conventions that must be followed
  • Sometimes running a Python project without specifying an entry file (more common on the command line, such as “Python -m http.server 8000”), that might exist__main__.pyFile, the package it is in is executed as a “file”

In summary, this means that Python is a scripting language that, unlike compiled languages, has a flexible choice of execution at the level of a single module (i.e., a.py file) or a package composed of multiple modules, unlike other languages that cannot be executed without an agreed entry point.

That is, There is no need for Python to dictate syntactically that programmers must define a single entry point (whether a function or a class or something).

Some of you may be confused because they often see or write code like this:

In # main is some body code
def main(a):...if __name__ == '__main__':
    main()
Copy the code

Isn’t that the Python main function? Believe that many students will think so!

Not too! Not too!

Except that the name of the function is “main,” it has nothing to do with the orthodox main function we introduced earlier, and it has nothing to do with enforcing or necessarily determining the order in which the program is executed. Without it, there are no grammar problems.

The reason some insiders have named a “main” function is to emphasize its “main” status, and to make it the first function to be executed. They might think it’s easier to remember a function named that way.

The reason some insiders write if __name__ == ‘__main__’ might be to indicate that main() is only run when the current script is executed directly, and not when it is intended to be imported into another module.

For those in the know, they have a point.

However, I personally do not recommend this writing method, and sometimes very disgusted!

The most obvious example: there are only a few dozen lines of code, or a single script file that implements a simple function (a little crawler, drawing a picture in Turtle, etc.), but they are all written in the same way.

Every time I see this kind of redundant code without thinking about it, I feel sick. Why write that if statement? If possible, the main function should be broken up without even wrapping it as a function.

My personal experience is as follows:

  • Think outside the box and write authentic code. The main entry function is specific to certain languages and should not be “copied” in Python. It should be written in a concise and elegant style that understands the characteristics of scripting languages
  • Use main.py instead of main(). Since Python’s program execution unit is a script file, not a function or class, it is recommended that the entry file be named main.py, and the internal functions be determined as required
  • Use it if you can__main__.pyAs an entry file. This file works well with the “-m” argument on the command line. Recommended Reading:Typical usage, rationale, and evolution of -m in Python
  • Writing is not recommendedif __name__ == '__main__'. First, if there is only one file, write is not recommended because there is no possibility of export. Second, when there are multiple files, this is not recommended in the entry file (main.py). The code logic of this file should be concise, and in theory its contents should not be exported to other modules because it is the starting point! Finally, multi-file non-entry files are also not recommended, because the main purpose of writing this judgment in a non-entry file is to write some test code, but the test code should be separated and written to a special directory or file.

Summary: This article first explains what the main entry function is and why some languages make it mandatory to write main. Next, it explains why Python doesn’t need to write the main function; Finally, I’ll share four of my own programming lessons for some people’s habitual misconceptions.

This article is part of the “Why” series (from Python Cat), which focuses on the syntax, design, and development of Python, and tries to show the magic of Python by asking “why” questions. Some of the topics will be introduced to the video version, please watch in B station, watch the address: video address

Public account [Python Cat], this number is a series of high-quality articles, such as Why Python series, cat star philosophy cat series, Python advanced series, good book recommendation series, technical writing, quality English recommendation and translation, etc., welcome to pay attention to oh.