這篇文章主要講解了“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è)提供源源不斷的流量和訂單咨詢。
當Python解釋器讀取Python文件時,它首先設(shè)置一些特殊變量。然后,它執(zhí)行文件中的代碼。
這些變量之一稱為__name__。
如果循序漸進地閱讀本文并閱讀其代碼片段,您將學習如何使用 if name== "main" ,以及它為什么如此重要。
Python文件稱為模塊,由.py文件擴展名標識。模塊可以定義函數(shù),類和變量。
因此,當解釋器運行模塊時,__name__將設(shè)置變量,就像 __main__正在運行的模塊是主程序一樣。
但是,如果代碼從另一個模塊導(dǎo)入該模塊,則該__name__ 變量將設(shè)置為該模塊的名稱。
讓我們看一個例子。創(chuàng)建一個名為的Python模塊file_one.py并將以下頂級代碼粘貼到其中:
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并將此代碼粘貼到其中:
print("File two __name__ is set to: {}" .format(__name__))
file_two.py 另外,file_one.py像這樣修改代碼,以便我們導(dǎo)入file_two模塊:
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:
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:
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:
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:
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為如下所示:
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:
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)