In today’s article, let’s discuss why some programming languages have a main function while Python doesn’t.
As we all know, there is no so-called “main function” in Python, yet you often come across articles online mentioning “Python’s main function” or “recommendations for writing a main function.”
In reality, they might be trying to imitate a real main function, but many people are misled (or misunderstand) and end up writing very awkward code.
Before we start the discussion, let’s answer the following two questions:
What exactly is meant by the so-called “main function”?
Why do some programming languages require a main function to be written?
Some programming languages treat the main function as the program’s execution entry point, such as C/C++, C#, Java, Go, Rust, etc. This function has specific implications:
- The main function name is mandatory, meaning there must be a main function.
- There can be at most one main function, meaning the program’s entry point is unique.
- The syntax format has specific requirements, and the way it’s written is relatively fixed.
Why must a main function be enforced as the entry point?
These languages are compiled languages that need to compile code into executable binary files. To allow the operating system/bootloader to locate the start of the program, such a function must be defined.
In short, it’s necessary to define a crucial starting point among a large amount of executable code.
It’s not hard to see that, for these languages, the main function is an indispensable component.
However, when we turn our attention to Python, the situation is quite different.
Python is an interpreted language, i.e., a scripting language. Its execution process is top-down, line by line, meaning its starting point is known.
Every .py file is an executable file and can serve as the entry point for the entire program. This means the program’s entry point is flexible and doesn’t need to adhere to any conventions.
Sometimes, running a Python project doesn’t require a designated entry file (common in command-line usage, such as python -m http.server 8000). This might be because the project has a main.py file, which is executed as a “file” within the package.
In summary, Python, as a scripting language, differs from compiled languages. Whether it’s a single module (i.e., a .py file) or a package composed of multiple modules, Python can choose a flexible execution method, which is entirely unlike other languages that must define an entry point.
In other words, Python doesn’t require programmers to define a unified entry point (whether it’s a function, class, or something else) syntactically.
Some students might feel confused because they often see or write code like this:
python
# main file
def main():
……
if __name__ == '__main__':
main()
Isn’t this Python’s main function? Many people believe so!
No, it’s not.
Apart from the function name being “main,” this code has nothing to do with the main function we discussed earlier. This function is neither mandatory nor does it determine the program’s execution order. Even without such a main function, there wouldn’t be any syntax issues.
The main reason people want to write a main function is actually to emphasize that this is the main function, hoping to artificially set it as the first function to execute.
They might think a function with this name is easier to remember.
The reason they write if __name__ == '__main__' might be to indicate that main() should only run when the current script is executed directly and not when it’s imported into other modules.
However, I personally don’t recommend this writing style.
Take a simple example: suppose there are only a few dozen lines of code, or a script file implements a simple function (a crawler, drawing a turtle, etc.), but it’s all written in the aforementioned way.
The if __name__ == '__main__' style is not recommended because:
First, if there’s only one file, it’s unlikely to be exported.
Second, if there are multiple files, it’s strongly advised not to write this statement in the entry file (main.py). Theoretically, its contents shouldn’t be exported for use by other modules because it’s the starting point.
Finally, in the case of multiple files, it’s also not recommended to write this statement in non-entry files because, at most, this statement can only include some test code. Even then, test code should be separated into dedicated directories or files.
Every time I see such awkward code, I feel uncomfortable. Why write such an if statement? You shouldn’t wrap this code into a function at all!
Summary
Break free from habitual thinking and write genuine code. The main function is the sole entry point in some languages but should not be used in Python. You should understand the characteristics of scripting languages and learn a simple and elegant style.
You can use main.py instead of writing a main function. Since Python’s execution unit is the script file, not a function or class, it’s recommended to name the entry file main.py and define functions inside it as needed.
Use main.py as the entry file. This file can be directly combined with the -m parameter in the command line.