溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python的if__name__ == __main__有什么作用

發(fā)布時間:2021-11-26 11:43:30 來源:億速云 閱讀:109 作者:iii 欄目:大數(shù)據(jù)

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

前期提要:
if__name__ == __main__

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

這些變量之一稱為__name__。

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

Python模塊介紹

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

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

但是,如果代碼從另一個模塊導入該模塊,則該__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像這樣修改代碼,以便我們導入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__,正在導入的所有其他模塊的變量將被設(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
 

當這樣的模塊被導入并運行時,它們的功能將被導入,并執(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
 

另外,您可以從導入的文件運行功能。為此,將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模塊確實很大,有很多功能(在我們的例子中有兩個),而您不想導入所有這些功能。修改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 要從模塊導入特定功能,請使用文件中的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__有什么作用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI