In order to understand the details of __name__ variable and the if condition, let us go through a simple exercise. Run a simple python file with just the following lines and run the file as python3 code,
为了了解__name__变量和if条件的详细信息,让我们进行一个简单的练习。 仅使用以下几行运行一个简单的python文件,并将其作为python3代码运行,
print("module_name :{}".format(__name__))
-bash-4.2$ python3 if_main.py
module_name :__main__
-bash-4.2$
In the above example, the output of the program states that the variable __name__ has a value of __main__. So, what happens is, in the background when python runs a file it goes through before it even runs any code, it sets a few special variables and 'name' is one of those special variables and when python runs the code it sets the '__name__' variable to '__main__'.
在上面的示例中,程序的输出表明变量__name__的值为__main__ 。 因此,发生的事情是,在后台运行python的文件时,甚至在运行任何代码之前都要经过它,它会设置一些特殊变量,而“ name”是这些特殊变量之一,而当python运行代码时,它将设置' __name__'变量为'__main__' 。
We can also import the modules, and when the modules are imported the variable __name__ is set to name the file.
我们还可以导入模块,并且在导入模块时,将变量__name__设置为文件名称。
Example:
例:
Create a file called second_module.py, and in second_module.py add the following lines and run the file.
创建一个名为second_module.py的文件,并在second_module.py中添加以下行并运行该文件。
import if_main
print("second module_name :{}".format(__name__))
-bash-4.2$ python3 second_module.py
module_name :if_main
second module_name :__main__
-bash-4.2$
In above example, we see that the imported file prints the file name and the second_module prints __main__, the reason being, the imported file is not run directly by python instead it is an imported file and hence the variable __name__ is set to the file name and the second_module is directly run by python and hence the variable __name__ is set to the __main__. Now returning to the subject of what does if __name__ == '__main__' do?
在上面的示例中,我们看到导入的文件打印了文件名, second_module打印了__main__ ,原因是,导入的文件不是直接由python运行,而是导入的文件,因此变量__name__被设置为文件名并且second_module直接由蟒运行并且因此可变__name__被设置为__main__。 现在回到__name__ =='__main__'怎么办的主题?
__name__ ==“ __main__”怎么办? (What does if __name__ == "__main__": do?)
When the above condition is checked, it is to assert if the file is directly run by python or is it being imported. The following example explains the usage of the if condition,
选中以上条件后,将声明该文件是直接由python运行还是正在导入。 以下示例说明了if条件的用法,
File 1 : if_main.py
文件1:if_main.py
def main():
print("module_name :{}".format(__name__))
if __name__ == "__main__":
main()
else:
print("run from import")
File 2 : second_module.py
文件2:second_module.py
import if_main
print("second module_name :{}".format(__name__))
-bash-4.2$ python3 second_module.py
run from import
second module_name :__main__
-bash-4.2$
-bash-4.2$ python3 if_main.py
module_name :__main__
-bash-4.2$
优点 (Advantages)
The reason to use this is ensuring to run some code only when it is directly run from the main file and some code will be executed only when it is imported.
使用此命令的原因是确保仅在直接从主文件运行时才运行某些代码,并且仅在导入时才执行某些代码。
The python file can be used either as a standalone program or a reusable module.
python文件可以用作独立程序或可重用模块。
翻译自: https://www.includehelp.com/python/what-does-if__name__-__main__-do.aspx