In Python code, we often see:
if __name__ == '__main__':
A simple explanation of the usage of this statement is: ifif __name__== '__main__'The module where it is located is run directly, then the code block under this statement is run, if the module it is located in is imported into otherrun in a python script, the code block under this statement will not be run.
For many programming languages, programs must have an entry, such as C, C++, and fully object-oriented programming languages Java, C#, etc.Among them, both C and C++ need a main function as the entry point of the program, that is, the operation of the program will start from the main function.Similarly, Java and C# must have a main class with a main method as the program entry.
However, unlike C, C++, Java, and C#, Python is a scripting language. Unlike compiled languages, the program is first compiled into binary and then run. Python is dynamically interpreted line by line, that is,It runs from the first line of the script, and there is no unified entry.
Also we know that there are two ways to use Python files:
The first: execute directly as a script
The second: import into other Python scripts to be called