One article to understand if __name__ == “__main__“: (Sprinkle water!)

        This article is a study excerpt and note recorded by the blogger for personal study, research or appreciation when he is studying in the fields of AI, drones, reinforcement learning and other fields. It is based on the blogger’s understanding of artificial intelligence and other fields. If there are any irregularities or infringements, they will be corrected immediately after being pointed out. We hope for your understanding. Article classification in AI learning :

       AI study notes (2) ---"One article to understand if __name__ == "__main__": (sprinkle water!)"

One article to understand if __name__ == "__main__": (sprinkle water!)

Table of contents

1 Let’s take a look at this example:

Xiaogang’s perspective, namely gang.py   

Xiaohong’s perspective, namely hong.py

2 Take a closer look:

3 Let’s go further:


        When most beginners read python code, they may see  if __name__ == "__main__":  this line of code. They may not know much about this line of code, especially for programming languages ​​​​such as C, C++, Java, and C#. When learning to switch to Python (haha, I am one of them).

        Without explaining the meaning of this line of code, let's give an example first.

1 Let’s take a look at this example:

        Xiaogang's good friend Xiaohong asked Xiaogang to eat snail noodles tomorrow. Xiaogang liked Xiaohong and agreed immediately, but Xiaogang actually didn't want to eat it.

        Treat both Xiaogang and Xiaohong as one .py file. Now Xiaohong and Xiaogang have to execute the event of going to eat snail noodles tomorrow. We use print to print it out.

Xiaogang

gang.py

#小刚
print("小刚明天去吃螺蛳粉")

Xiaohong

hong.py 

#小红
print("小红明天去吃螺蛳粉")

Xiaogang’s perspective, namely gang.py   

        Since Xiaogang didn't want to eat snail noodles, and Xiaogang didn't want Xiaohong to know, Xiaogang could only think about it in his mind.

        If you think about it, put it  behind  if __name__ == '__main__': 

Add         in gang.py

        if __name__ == '__main__':

        Then he will do what he wants

#小刚
print("小刚明天去吃螺蛳粉")

if __name__ == '__main__':
    print('小刚心里想:不想吃螺蛳粉')

 operation result:

We can see that the if __name__ == '__main__':  print is  executed here and prints out

Xiaogang thought to himself: I don’t want to eat snail noodles


Xiaohong’s perspective, namely hong.py

Xiaohong invited Xiaogang to eat snail noodles, and regarded this action as using  import to  import  gang.py.

#小红
import gang
print("小红明天去吃螺蛳粉")

 operation result:

It can be seen that Xiaohong only knows that Xiaogang and she are going to eat snail noodles, but does not know what Xiaogang really thinks.

        Xiaogang thought to himself: I don’t want to eat snail noodles.

        From a code level, hong.py uses import when calling gang.py.

        if __name__ == '__main__': The previous statement is executed, but the following statement is not executed.


2 Take a closer look:

We use print to  print out __name__  before if __name__=="__main__": in the gang.py script .

#小刚
print("小刚明天去吃螺蛳粉")
print(__name__)
if __name__ == '__main__':
    print('小刚心里想:不想吃螺蛳粉')

The running results are as follows:

What about executing hong.py again? The running results are as follows:

It can be observed:

  • After gang.py is executed, the value of variable __name__ is "__main__"
  • After hong.py is executed, the value of variable __name__ is "gang". Returning to the if condition judgment itself, because the condition of __name__=="__main__" is not met, the subsequent code cannot be executed.

The principle is as follows:

        Since every Python file contains the built-in variable __name__, when the run module is executed, __name__ is equal to the file name (including the suffix .py).

        If imported into another module, __name__ is equal to the module name (without the suffix .py).

        And "__main__" is equal to the name of the currently executed file (including the suffix .py). ("__main__" = filename.py )

        So when the module is executed directly, the result of __name__ == '__main__' is true ; and when the module is imported into other modules, the result of __name__ == '__main__' is false , that is, the corresponding method is not called.

        In short: __name__ is the name of the current module. When the module is run directly, __main__ is used to represent the module name. If the if judgment is satisfied , the following code will be run.

        When the module is imported, the code that is not satisfied by the if judgment will not be run.


3 Let’s go further:

        Involves the concept of a program entry

        For many programming languages, the program must have an entry point, such as C, C++, Java, C#, etc.

        Both C and C++ need a main function as the entry point of the program, that is, the running 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 what is different from C, C++, Java and C# is that Python is a scripting language. Unlike compiled languages, the program is first compiled into a binary and then run. Python is dynamically interpreted and run line by line, that is, starting from the first step of the script. A line starts running, and there is no unified entry.

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

  1. The first: execute directly as a script
  2. The second type: import into other python scripts to be called (module reuse) for execution, that is, to be imported and executed as a module (library)

       However, whether it is run directly or imported, the top-level code will be run (Python uses indentation to distinguish code levels). But in actual use, when the python file is called and executed by other python scripts as a module, there are some codes that we do not want to be run.

        The function of if __name__ == '__main__' is to define the execution method of the execution code in these two situations. 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.


So do you understand? friends

Part of this article is copied and borrowed from the following two articles. You can take a look:

The function and principle of if __name__ == 'main' in Python programming

Detailed explanation of if __name__ == '__main__'

     If there is any inappropriateness or inaccuracy in the article, I hope you will understand and point it out. Since some texts, pictures, etc. come from the Internet, the true source cannot be verified. If there is any related dispute, please contact the blogger to delete it. If there are any errors, questions or infringements, please leave a comment to contact the author, or follow the VX public account: Rain21321 to contact the author.

Guess you like

Origin blog.csdn.net/qq_51399582/article/details/133673351