Why doesn’t Python have a main function?

By Pawan Jain

Pawan Jain
Image for post
Image for post
Photo by Paul Hanaoka on Unsplash

There is no doubt that there is no so-called main entry function in Python, but there are often articles on the Internet that mention “Python’s main function” and “suggest to write the main function.”

Their intention may be to imitate the authentic main functions, but many people are misled (or misunderstood) and write very cumbersome code.

In this story of the “Why Python” series, we will talk about Why Python does not have the main function?. You will get the link to the previous two stories of this series at the end.

Before starting the topic, let’s answer these two questions:

  • What does the so-called “main function” mean?
  • Why do some programming languages ​​need to force writing a main function?

Some programming languages ​​use the main function as the execution entry of the program, such as C/C++, C#, Java, Go, Rust, etc., which have specific meanings:

  • This main function name is mandatory, which means that there must be a primary function.
  • There can be at most one main function, which means that the entry to the program is unique.
  • The grammar format has specific requirements, with a relatively fixed template.

These languages are compiled languages, which require code to be compiled into executable binaries in order for the operating system/bootloader to find The start of the program, so you need to define this one function.

Simply, there is a significant beginning that needs to be defined in a large pile of code that can be used for execution.

It is not difficult to see that the main function is an essential and indispensable organic part of those languages.

However, when we look at Python again, the situation is quite different.

  • Python is an interpreted language, i.e., a scripting language. The running process is from top to bottom, line by line, which means that its starting point is known.
  • Each .py file is an executable file, which can be used as the entry file of the entire program, implies the entry of the program is flexible, and no convention must be followed
  • Sometimes running Python project, did not specify the entry file (the command line more common, such as “python -m http.server 8000”), it may be the presence of main.pya file, it is in the package as a "file" to perform the

In summary, it means that the scripting language Python is different from the compiled language. Whether it is at the level of a single module (that is, a .py file), or at the package level composed of multiple modules, it can choose a flexible execution method, Unlike other languages ​​that cannot be implemented without a well-defined entry.

In other words, Python does not need to stipulate that the programmer must define a unified entry (whether it is a function or a class or something) at the syntax level.

Some students may be confused because they often see or write the following code themselves:

# main filedef main():


if __name__ == '__main__':

Isn’t this the main function of Python? I believe many of us think so!

No, It’s not

Except that the function name is “mainit does not have a half-cent relationship with the orthodox main function we introduced earlier, neither mandatory nor necessarily determining the order of program execution. Without it, it will not cause any grammatical problems.

The reason why some people want to name a “main” function is actually to emphasize its “main” status and wants to arrange it as the first function to executed artificially.

They may think that such named functions are easier to remember.

The reason why they want to write if __name__ == '__main__', may want to show that main() only runs when the current script is directly executed and doesn’t want to be run when it is imported into other modules.

However, I personally do not recommend this way of writing

The most obvious example: there are only a few dozen lines of code or just a script file that implements a simple function (a small crawler, drawing a picture with a turtle, etc.), but they’re all written in the same style as before.

It is not recommended to write if name__ == ‘__main’

  • First of all, if there is only one file because there is no possibility of export.
  • Secondly, if there are multiple files, it is highly discouraged to write this sentence in the entry file (main.py). Theoretically, its content shouldn’t be exported for use in other modules, since it’s the starting point.
  • Finally, it is also not recommended to write this judgment in non-entry files with multiple files, as the most that can be done is to write some of these test codes. Still, the test code should be separated and written in a dedicated directory or file.

Every time I see this cumbersome code without thinking, I feel uncomfortable. Why write that if statement? If possible, you should split the main function without even wrapping it into a function!

  • Break inertial thinking and write authentic code. The main entry function is unique to some languages, and should not be used in Python. You should understand the characteristics of the scripting language and write a simple and elegant style.
  • Use main.py instead of main(). Because the program execution unit of Python is a script file, not a function or class, it is recommended to name the entry file main.py, and the internal functions are determined according to requirements.
  • If possible, use main.py as an entry file. This file is straightforward to use in combination with the "-m" parameter on the command line.