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