Explanation of if __name__ == '__main__' in python

program entry

For many programming languages, the program must have an entry, such as C, C++, and fully object-oriented programming languages ​​​​Java, C#, etc. 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 containing the Main method as the program entry point.

But different from 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 and run line by line, that is, from the first One line starts running, there is no unified entry.

In addition, we know that there are two ways to use python files:

The first type: execute directly as a script
The second type: import to other python scripts to be called (module reuse) to execute, that is, to be imported and executed as a module (library), whether it is
run directly or imported, the topmost code will be executed Run (Python uses indentation to distinguish code levels). But in actual use, when a python file is called and executed by other python scripts as a module, there are some codes that we don't want to be run.

The function of if __name__ == '__main__' is to define the execution mode of the execution code in these two cases. The code under this statement will only be executed when the file is directly executed as a script, and it is not imported into other scripts. will be executed.

the case

For example:
To calculate the area of ​​a circle, the formula is S = л * r^2 (where r is the radius)

Step 1: First create a param.py file, where the code is as follows:

pi = 3.1415926

def main():
    print("pi:", pi)
 
main()

A parameter pi is defined in the param.py file, and the output of the file is directly executed:

pi: 3.1415926

Step 2: Then create the calculate.py file to calculate the area of ​​the circle. The pi parameter in the param.py file needs to be used in this file. We need to import the pi variable from param.py into calculate.py:

from param import pi
 
def area_calculate(r):
    s = pi * (r ** 2)
    return s
 
def main():
    print("The area of the circle: ", area_calculate(2))

main()

Run calculate.py (python calculate.py), the output is:

pi: 3.1415926
The area of the circle:  12.5663704

It can be seen from the output that the main function in param.py has also been executed. In fact, we don’t want it to be executed. At this time, we can if __name__ == '__main__' modify param.py with:

pi = 3.1415926

def main():
    print("pi:", pi)

if __name__ == "__main__":
    main()

Running param.py again, the output is as follows:

pi: 3.1415926

Running calculate.py again, the output is as follows:

The area of the circle:  12.5663704

It can be seen from the comparison that if param.py is run again, the code before and after the if __name__ == '__main__' statement is executed, and the output pi: 3.1415926 is still output, but when calculate.py is run again, if __name__ = in the imported param.py = The code after the '__main__' statement has not been executed, and the output no longer contains pi: 3.1415926.

if __name__ == '__main__' is equivalent to the program entry of Python simulation. Python itself does not stipulate to write this way, it is just a coding habit. Due to mutual reference between modules, different modules may have such definitions, but the entry program can only have one. Which entry program is selected depends on the value of __name__ . And __name__ is a built-in variable used to represent the name of the current module.

Operating principle

Each python module (python file, that is, param.py and calculate.py here) contains a built-in variable __name__ , which is equal to the file name (including the suffix .py) when the running module is executed ; if imported to In other modules, __name__ is equal to the module name (without the suffix .py). And '__main__' is equal to the name of the current execution file (including the suffix .py). Then when the module is executed directly, __name__ == '__main__' is true, so subsequent code can continue to execute.

for example. We add print(__name__) before if __name__ == '__main__'
in the param.py script , that is, print out __name__ . The content of the file is as follows:

pi = 3.1415926

def main():
    print("pi:", pi)

print(__name__)

if __name__ == "__main__":
    main()


The output of the running result:

__main__
pi: 3.1415926

It can be seen that the value of the variable __name__ is '__main__' at this time .

Then execute calculate.py, the execution result is as follows:

param
The area of the circle:  12.5663704

At this time, the __name__ variable value in param.py is param, and the condition of __name__ == '__main__' is false, so the subsequent code cannot be executed.
 

Guess you like

Origin blog.csdn.net/qq_43241216/article/details/124458655