DEV Community

Cover image for Why doesn’t Python have a main function?
Pawan Jain
Pawan Jain

Posted on • Originally published at towardsdatascience.com

Why doesn’t Python have a main function?

Why don’t I recommend it either

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, we will talk about Why Python does not have the main function?.

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.

Why do you have to force a main entry function?

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 the 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.py file, it is in the package as a "file" to perform the operations.

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 people may be confused because they often see or write the following code themselves:

# main file
def main():
    ……
if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

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 “main” it 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' because of the following reasons.

  • 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!

To sum up

  • 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.

How do you feel about after reading this article? Do you like to use that if statement? Let me know in comments with the reason 😊

Top comments (1)

Collapse
 
simonrouse9461 profile image
Chuhan Feng

One of the most important reasons of using def main() is to avoid hidden bugs caused by global variables.