Why is there no main() function in Python?

In this article, we will learn Why is there no main() function in Python. There is no doubt that Python has NO so-called main function, however, articles on the Internet frequently reference "Python's main function" and "suggest writing the main function....

In this article, we will learn Why is there no main() function in Python.

There is no doubt that Python has NO so-called main function, however, articles on the Internet frequently reference "Python's main function" and "suggest writing the main function."

Their purpose may be to replicate the original primary methods, but many individuals are misled (or misunderstood) and create extremely complicated code as a result.

Before we begin, we will answer the following two questions −

  • What exactly is the "main function"?

  • Why do some programming languages need the use of the main function?

Some programming languages, such as C/C++, C#, J**a, Go, Rust, and others, employ the main function as the program's execution entry, which has specific meanings −

  • This main function name is required, implying that there must be a primary function.

  • Because there can only be one main function, each entry into the program is unique.

  • The syntax format has specific requirements and follows a very consistent(fixed) template.

Why do you h**e to make the main entry function mandatory?

These are compiled languages, which require code to be compiled into executable binaries for the operating system/bootloader to find the program's start, therefore you must define this one function.

Simply said, there is a significant beginning that must be defined in a vast pile of executable code.

The main function is an essential organic feature of those languages.

However, when we return to Python, the picture changes dramatically.

  • Python is an interpreted language, also known as a scripting language. The running process runs from top to bottom, line by line, which means that its starting point is known.

  • Each .py file is an executable file that can be used as the program's entry point, implying that the program's entry point is flexible and no convention must be followed.

  • When executing the Python project without specifying the entry file (the more typical command line, such as "python -m http.server 8000"), it may be due to the presence of the main.py file, which is in the package as a "file" to do the operations.

In summary, the scripting language Python differs from the compiled language. It can choose a flexible execution technique at the level of a single module (that is, a.py file), or at the package level composed of many modules, unlike other languages that cannot be implemented without a well-defined entry.

In other words, Python does not require programmers to specify a unified entry (whether it is a function, a class, or some other) at the syntactic level.

Some users may be puzzled since they frequently encounter or write the following code −

# defining the main function using the def keyword
def main():
   ……
if __name__ == '__main__':
   main()

Isn't this Python's main function? I believe many of us agree! −

No, it is not.

Except for the fact that the function name is "main," it has no link to the orthodox main function we described earlier, and it is not required nor necessarily controlling the order of program execution. It will not cause any syntactic issues without it.

Some individuals desire to identify a "main" function to stress its "main" status and to place it as the first function to be executed artificially.

They may believe that functions with such names are easier to remember.

They may wish to write if name =='main' to demonstrate that main() only runs when the current script is directly executed and should not be executed when it is imported into other modules.

However, We do not suggest this style of writing.

The most common example: there are simply a few dozen lines of code or a script file that implements a simple function (a small crawler, painting a picture with a turtle, etc.), but they are all written in the same way as before.

Because of the following reasons, it is not suggested to write if name__ == '__main' −

  • First and foremost, if there is only one file because there is no export option.

  • Second, if there are several files, writing this sentence in the entry file(main.py) is strongly discouraged. Because it is the starting point, its content should not be exported for use in other modules.

  • Finally, writing this judgment in non-entry files with numerous files is not suggested, as the most that can be done is to create some of these test programs. However, the test code should be separated and stored in a separate directory or file.

I am worried every time I encounter this complex code without thinking about it. Why did you write that if statement? If at all possible, split the main function without even wrapping it in a function!

To summarise

  • Break out from inertial thinking and build authentic code. The main entry function is unique to some languages and should not be used in Python. You should comprehend the features of the scripting language and write in a simple and appealing style.

  • Use main.py instead of main(). Because Python's program execution unit is a script file rather than a function or class, the entry file should be named main.py, and internal functions are decided by requirements.

  • If feasible, use main.py as the entry file. This file is simple to use in combination with the "-m" parameter on the command line.

Conclusion

In this article, we learned why Python, unlike other programming languages, lacks a main() function. We also learned about the condition __name__ == '__main__'.

评论0

首页 导航 会员 客服