如果__name__ =='__main__':在Python中怎么办?
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條件的詳細(xì)信息,讓我們進(jìn)行一個(gè)簡(jiǎn)單的練習(xí)。 僅使用以下幾行運(yùn)行一個(gè)簡(jiǎn)單的python文件,并將其作為python3代碼運(yùn)行,
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__ 。 因此,發(fā)生的事情是,在后臺(tái)運(yùn)行python的文件時(shí),甚至在運(yùn)行任何代碼之前都要經(jīng)過它,它會(huì)設(shè)置一些特殊變量,而“ name”是這些特殊變量之一,而當(dāng)python運(yùn)行代碼時(shí),它將設(shè)置' __name__'變量為'__main__' 。
We can also import the modules, and when the modules are imported the variable __name__ is set to name the file.
我們還可以導(dǎo)入模塊,并且在導(dǎo)入模塊時(shí),將變量__name__設(shè)置為文件名稱。
Example:
例:
Create a file called second_module.py, and in second_module.py add the following lines and run the file.
創(chuàng)建一個(gè)名為second_module.py的文件,并在second_module.py中添加以下行并運(yùn)行該文件。
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?
在上面的示例中,我們看到導(dǎo)入的文件打印了文件名, second_module打印了__main__ ,原因是,導(dǎo)入的文件不是直接由python運(yùn)行,而是導(dǎo)入的文件,因此變量__name__被設(shè)置為文件名并且second_module直接由蟒運(yùn)行并且因此可變__name__被設(shè)置為__main__。 現(xiàn)在回到__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運(yùn)行還是正在導(dǎo)入。 以下示例說明了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$優(yōu)點(diǎn) (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.
使用此命令的原因是確保僅在直接從主文件運(yùn)行時(shí)才運(yùn)行某些代碼,并且僅在導(dǎo)入時(shí)才執(zhí)行某些代碼。
The python file can be used either as a standalone program or a reusable module.
python文件可以用作獨(dú)立程序或可重用模塊。
翻譯自: https://www.includehelp.com/python/what-does-if__name__-__main__-do.aspx
總結(jié)
以上是生活随笔為你收集整理的如果__name__ =='__main__':在Python中怎么办?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# uri.host_C#| Uri.
- 下一篇: python方法items_Python