What is the use of Python's if__name__ = = _ _ main__
This article mainly explains the "Python if__name__ = = _ main__ what is the role", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Python if__name__ = = _ main__ what is the role of it?"
Previous feed: if__name__ = = _ _ main__
When the Python interpreter reads the Python file, it first sets some special variables. It then executes the code in the file.
One of these variables is called _ _ name__.
If you read this article step by step and read its code snippet, you will learn how to use if name = = "main" and why it is so important.
Introduction of Python module
The Python file is called a module and is identified by the .py file extension. Modules can define functions, classes, and variables.
Therefore, when the interpreter runs the module, _ _ name__ sets the variable, just as if the running module of _ _ main__ is the main program.
However, if the code imports the module from another module, the _ _ name__ variable is set to the name of the module.
Let's look at an example. Create a Python module named file_one.py and paste the following top-level code into it:
Python file one moduleprint ("File one _ name__ is set to: {}" .format (_ _ name__))
File_one.py by running this file, you will know exactly what we are talking about. The variable of the _ _ name__ module is set to _ _ main__:
File one _ _ name__ is set to: _ _ main__
Now add another file named file_two.py and paste this code into it:
Python module to importprint ("File two _ name__ is set to: {}" .format (_ _ name__))
File_two.py in addition, file_one.py modifies the code like this so that we can import the file_two module:
Python module to executeimport file_two
Print ("File one _ name__ is set to: {}" .format (_ _ name__))
When file_one.py file_one runs our code again, the _ _ name__ variable file_one in the display remains unchanged and is still set to _ _ main__. But now the variable _ _ name__in file_two is set to its module name, so file_two.
The result should be as follows:
File two _ _ name__ is set to: file_twoFile one _ _ name__ is set to: _ _ main__
But if file_two runs directly, you will see that its name is set to _ _ main__:
File two _ _ name__ is set to: _ _ main__
The variable of the file / module used by name__ to run will always be _ _ main. But the variables of all other modules being imported by _ _ name__, will be set to the names of their modules.
The Python file naming convention uses the commonly used methods _ _ name__ and _ _ main__ to look like this:
If _ name__ = = "_ _ main__":
Do something here
Let's take a look at how it works in real life and how to actually use these variables.
Make the modification file_one,file_two as follows:
File_one:
Python module to executeimport file_two
Print ("File one _ name__ is set to: {}" .format (_ _ name__))
If _ _ name__ = "_ _ main__": print ("File one executed when ran directly") else: print ("File one executed when imported")
File_one.py file_two:
Python module to importprint ("File two _ name__ is set to: {}" .format (_ _ name__))
If _ _ name__ = "_ _ main__": print ("File two executed when ran directly") else: print ("File two executed when imported")
File_two.py again, at run time, file_one you will see that the program recognizes which of these two modules, _ _ main__, and executes the code according to our first if else statement.
The result should be as follows:
File two _ _ name__ is set to: file_twoFile two executed when importedFile one _ _ name__ is set to: _ _ main__File one executed when ran directly
Now run file_two, and you will see that the _ _ name__ variable is set to _ _ main__:
File two _ _ name__ is set to: _ _ main__File two executed when ran directly
When such modules are imported and run, their functions are imported and the top-level code is executed.
To see the actual effect of this process, modify the file as follows:
File_one:
Python module to executeimport file_two
Print ("File one _ name__ is set to: {}" .format (_ _ name__))
Def function_one (): print ("Function one is executed")
Def function_two (): print ("Function two is executed")
If _ _ name__ = "_ _ main__": print ("File one executed when ran directly") else: print ("File one executed when imported")
File_one.py file_two:
Python module to importprint ("File two _ name__ is set to: {}" .format (_ _ name__))
Def function_three (): print ("Function three is executed")
If _ _ name__ = "_ _ main__": print ("File two executed when ran directly") else: print ("File two executed when imported")
The feature is now loaded but cannot be run.
To run one of these functions, modify part of if name = = "main" to file_one as follows:
If _ _ name__ = "_ _ main__": print ("File one executed when ran directly") function_two () else: print ("File one executed when imported")
At run time, file_one you should see something like this:
File two _ _ name__ is set to: file_twoFile two executed when importedFile one _ _ name__ is set to: _ _ main__File one executed when ran directlyFunction two is executed
In addition, you can run the function from the imported file. To do this, modify the if name = "main" section to file_one as follows:
If _ _ name__ = "_ _ main__": print ("File one executed when ran directly") function_two () file_two.function_three () else: print ("File one executed when imported")
You can expect this result:
File two_ _ name__ is set to: file_two_step_3File two executed when importedFile one _ _ name__ is set to: _ _ main__File one executed when ran directlyFunction two is executedFunction three is executed
Now let's say that the file_two module is really big and has a lot of features (two in our example), and you don't want to import all of them. Modify the file_two to look like this:
Python module to importprint ("File two _ name__ is set to: {}" .format (_ _ name__))
Def function_three (): print ("Function three is executed")
Def function_four (): print ("Function four is executed")
If _ _ name__ = "_ _ main__": print ("File two executed when ran directly") else: print ("File two executed when imported")
File_two.py to import specific features from a module, use the fromimport block file_one in the file:
Python module to executefrom file_two_step_3 import function_three
Print ("File one _ name__ is set to: {}" .format (_ _ name__))
Def function_one (): print ("Function one is executed")
Def function_two (): print ("Function two is executed")
If__name__ = = "_ _ main__": print ("File one executed when ran directly") function_two () function_three () else: print ("File one executed when imported") Thank you for your reading. This is the content of "what is the function of Python's if__name__ = = _ _ main__". After the study of this article I believe that you have a deeper understanding of the role of Python if__name__ = = _ main__, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!