without doubt Python There is no such thing as main Entry function , But I often see some articles on the Internet “Python Of main function ”、“ Suggest to write main function ”……
Some people know it , His intention may be to imitate those authentic main function , But a lot of people are obviously misled ( Or I misunderstood ), It's very cumbersome code to write .
In this issue “Python Why? ” Let's talk about it Python Why not? main function ?
Before we get to the point , Let's first answer these two questions : So-called “main function ” What is it? ? Why do some programming languages have to force a main function ?
Some programming languages use main Function as the execution entry of the program , for example C/C++、C#、 Java、Go and Rust etc. , They have a specific meaning :
Why force a main The entry function ?
These are compiled languages , You need to compile the code into executable binaries , To make the operating system / The starter finds the starting point of the program , So we have to agree on this function . In short , It's in a lot of code , You need to define a significant start for execution .
It's not hard to see. ,main Functions are an important and indispensable part of those languages .
However , Let's see Python, The situation is very different .
It comes down to , mean Python This scripting language is different from compiled languages , It doesn't matter at the single module level ( That is, a .py file ), Or at the package level of multiple modules , You can choose flexible execution methods , Unlike other languages, they can't execute without the agreed entry .
in other words ,Python There is no need to specify at the syntax level that programmers must define a unified entry ( Whether it's a function or a class or something ).
Some students may have doubts , Because they often see or write the following code themselves :
# main There's some body code in it
def main():
……
if __name__ == '__main__':
main()
Isn't that Python Of main Function ? I believe many students will think so !
no ! no !
Except that the function name is “main” outside , It's the same as the orthodox main Functions have nothing to do with half a cent , There is no compulsion , Nor does it necessarily determine the sequence of program execution . It's missing , It won't lead to any grammar problems .
The reason why some insiders want to name one ”main“ function , Actually, I want to emphasize it ” The main “ status , Want to artificially arrange it as the first function to execute . They might think of a function named like this , It's easier to remember .
The reason why some insiders want to write if name == '__main__' , May want to show main() Run only if the current script is executed directly , Do not want to be imported into other modules to run .
For these “ insider ”, They have a point .
however , I personally don't recommend it , And sometimes it's very disgusting !
The most obvious example : There are only dozens of lines of code , Or just one script file , Implement a simple function ( A little bit of a reptile 、 use turtle Draw a picture and so on ), But they're all written in the previous style .
Every time I see this kind of unthinkable and cumbersome code , I feel sick . Why write that line if Sentence? ? If possible , It should be split main function , It doesn't even have to be encapsulated as a function !
I personally sum up the following experience :
Summary : This paper first explains what is main Entry function , And why some languages are forced to write main function ; next , Explained why Python No need to write main function ; Finally, it is aimed at some people's habitual misunderstanding , I share four personal programming experiences .
The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing