欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Python的if__name__==__main__有什么作用

這篇文章主要講解了“Python的if__name__ == __main__有什么作用”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Python的if__name__ == __main__有什么作用”吧!

創(chuàng)新互聯(lián)成立于2013年,我們提供高端網(wǎng)站建設(shè)成都網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)站定制、成都全網(wǎng)營銷推廣小程序制作、微信公眾號開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為成都廣告推廣企業(yè)提供源源不斷的流量和訂單咨詢。

前期提要:
if__name__ == __main__

當Python解釋器讀取Python文件時,它首先設(shè)置一些特殊變量。然后,它執(zhí)行文件中的代碼。

這些變量之一稱為__name__。

如果循序漸進地閱讀本文并閱讀其代碼片段,您將學習如何使用 if name== "main" ,以及它為什么如此重要。 

Python模塊介紹

Python文件稱為模塊,由.py文件擴展名標識。模塊可以定義函數(shù),類和變量。

因此,當解釋器運行模塊時,__name__將設(shè)置變量,就像   __main__正在運行的模塊是主程序一樣。

但是,如果代碼從另一個模塊導(dǎo)入該模塊,則該__name__  變量將設(shè)置為該模塊的名稱。

讓我們看一個例子。創(chuàng)建一個名為的Python模塊file_one.py并將以下頂級代碼粘貼到其中: 

Python file one module

print("File one __name__ is set to: {}" .format(__name__))
 

file_one.py 通過運行此文件,您將確切了解我們在說什么。該__name__模塊的變量設(shè)置為__main__:

File one __name__ is set to: __main__
 

現(xiàn)在添加另一個名為的文件file_two.py并將此代碼粘貼到其中: 

Python module to import

print("File two __name__ is set to: {}" .format(__name__))
 

file_two.py 另外,file_one.py像這樣修改代碼,以便我們導(dǎo)入file_two模塊: 

Python module to execute

import file_two
print("File one __name__ is set to: {}" .format(__name__))
 

file_one.py file_one再次運行我們的代碼將顯示中的__name__變量file_one沒有更改,并且仍然設(shè)置為__main__。但是現(xiàn)在變量__name__in file_two被設(shè)置為其模塊名稱,因此file_two。

結(jié)果應(yīng)如下所示:

File two __name__ is set to: file_twoFile one __name__ is set to: __main__
 

但是file_two直接運行,您會看到其名稱設(shè)置為__main__:

File two __name__ is set to: __main__
 

name__用于運行的文件/模塊的變量將始終為__main。但是__name__,正在導(dǎo)入的所有其他模塊的變量將被設(shè)置為其模塊的名稱。

Python文件命名約定 使用通常使用的方法__name__和__main__看起來像這樣:

if __name__ == "__main__":
 

Do something here

讓我們看看它在現(xiàn)實生活中是如何工作的,以及如何實際使用這些變量。

進行修改file_one,file_two如下所示:

file_one: 

Python module to execute

import 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 import

print("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 同樣,在運行時,file_one您將看到程序識別出這兩個模塊中的哪個模塊,__main__并根據(jù)我們的第一條if else語句執(zhí)行了代碼。

結(jié)果應(yīng)如下所示:

File two __name__ is set to: file_twoFile two executed when importedFile one __name__ is set to: __main__File one executed when ran directly
 

現(xiàn)在運行file_two,您將看到該__name__變量設(shè)置為__main__:

File two __name__ is set to: __main__File two executed when ran directly
 

當這樣的模塊被導(dǎo)入并運行時,它們的功能將被導(dǎo)入,并執(zhí)行頂層代碼。

要查看此過程的實際效果,請將文件修改為如下所示:

file_one: 

Python module to execute

import 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 import

print("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")
 

現(xiàn)在,功能已加載但無法運行。

要運行這些功能之一,請將其中的if name== "main"一部分修改為file_one如下所示:

if __name__ == "__main__":   print("File one executed when ran directly")   function_two()else:   print("File one executed when imported")
 

運行時,file_one您應(yīng)該看到應(yīng)該是這樣的:

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
 

另外,您可以從導(dǎo)入的文件運行功能。為此,將if name== “main”部分修改為file_one如下所示:

if __name__ == "__main__":   print("File one executed when ran directly")   function_two()   file_two.function_three()else:   print("File one executed when imported")
 

您可以期望這樣的結(jié)果:

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
 

現(xiàn)在讓我們說file_two模塊確實很大,有很多功能(在我們的例子中有兩個),而您不想導(dǎo)入所有這些功能。修改file_two為如下所示: 

Python module to import

print("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 要從模塊導(dǎo)入特定功能,請使用文件中的fromimport塊file_one: 

Python module to execute

from 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")

感謝各位的閱讀,以上就是“Python的if__name__ == __main__有什么作用”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Python的if__name__ == __main__有什么作用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

當前題目:Python的if__name__==__main__有什么作用
網(wǎng)頁地址:http://aaarwkj.com/article36/phdisg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、云服務(wù)器App開發(fā)、網(wǎng)站營銷、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
日本人妻丰满熟妇久久| 欧美男女精品一区二区三区| 国产区二区三区在线视频| 少妇又色又爽又高潮欧美| 日韩av裸体在线播放| 国产国产乱老熟视频网站| 亚洲综合成人av在线| 亚洲激情午夜福利视频| 精品人妻av区天天看片| 婷婷色悠悠,色悠悠激情啪啪 | 亚洲熟女av一区少妇| 色哟国产传媒视频在线观看| 麻豆视传媒短视频网站免费| 国产精品黄色91熟女| 免费人成在线观看网站免费观看| 日本中文字幕免费一区| 天天操夜夜夜夜夜操| 亚洲国产中日韩精品综合| 日本在线一区二区不卡视频| 久久久久久97精品| 亚洲欧洲日本在线天堂| 亚洲中文字幕高清无二区| 91大神午夜在线观看| 欧美黄片视频在线免费看| 亚洲成av人片又粗又长| 亚洲香蕉av在线一区二区三区| 精品国产一区二区三区四不卡在线| av资源中文字幕在线天堂| 一区二区人妻乳中文字幕| 国产一区黄片视频在线观看| 日韩国产精品视频二区| 黄色亚洲大片免费在线观看| 在线播放欧美视频91| av二区不卡国产精品| 国产三级无遮挡在线观看| 久久草福利视频在线观看| 国产三级av高清一区二区| 国产亚洲加勒比久久精品| 五月天色婷婷亚洲综合一区| 亚洲av最近在线观看| 亚洲丰满熟女乱一区二区三区|