日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

如果__name__ =='__main__':在Python中怎么办?

發(fā)布時(shí)間:2023/12/1 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如果__name__ =='__main__':在Python中怎么办? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。