廢話很少說,直接上貨....python
當Python解釋器讀取Python文件時,它首先設置一些特殊變量。而後,它執行文件中的代碼。web
這些變量之一稱爲__name__。算法
若是按部就班地閱讀本文並閱讀其代碼片斷,您將學習如何使用 if name == "main" ,以及它爲何如此重要。編程
Python模塊介紹
Python文件稱爲模塊,由.py文件擴展名標識。模塊能夠定義函數,類和變量。微信
所以,當解釋器運行模塊時,__name__將設置變量,就像 __main__正在運行的模塊是主程序同樣。app
可是,若是代碼從另外一個模塊導入該模塊,則該__name__ 變量將設置爲該模塊的名稱。編輯器
讓咱們看一個例子。建立一個名爲的Python模塊file_one.py並將如下頂級代碼粘貼到其中:函數
Python file one module
print("File one __name__ is set to: {}" .format(__name__))
file_one.py 經過運行此文件,您將確切瞭解咱們在說什麼。該__name__模塊的變量設置爲__main__:學習
File one __name__ is set to: __main__
如今添加另外一個名爲的文件file_two.py並將此代碼粘貼到其中:flex
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沒有更改,而且仍然設置爲__main__。可是如今變量__name__in file_two被設置爲其模塊名稱,所以file_two。
結果應以下所示:
File two __name__ is set to: file_twoFile one __name__ is set to: __main__
可是file_two直接運行,您會看到其名稱設置爲__main__:
File two __name__ is set to: __main__
name__用於運行的文件/模塊的變量將始終爲__main。可是__name__,正在導入的全部其餘模塊的變量將被設置爲其模塊的名稱。
Python文件命名約定 使用一般使用的方法__name__和__main__看起來像這樣:
if __name__ == "__main__":
Do something here
讓咱們看看它在現實生活中是如何工做的,以及如何實際使用這些變量。
進行修改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__並根據咱們的第一條if else語句執行了代碼。
結果應以下所示:
File two __name__ is set to: file_twoFile two executed when importedFile one __name__ is set to: __main__File one executed when ran directly
如今運行file_two,您將看到該__name__變量設置爲__main__:
File two __name__ is set to: __main__File two executed when ran directly
當這樣的模塊被導入並運行時,它們的功能將被導入,並執行頂層代碼。
要查看此過程的實際效果,請將文件修改成以下所示:
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")
如今,功能已加載但沒法運行。
要運行這些功能之一,請將其中的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您應該看到應該是這樣的:
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")
您能夠指望這樣的結果:
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
如今讓咱們說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")
file_one.py
結論
該__name__變量有一個很是好的用例,不管您是想要一個能夠做爲主程序運行仍是能夠由其餘模塊導入的文件。if name == "main"導入模塊時,咱們可使用塊來容許或阻止部分代碼運行。
當Python解釋器讀取文件時,將__name__變量設置爲__main__好像正在運行的模塊,或者將其設置爲模塊的名稱(若是已導入)。讀取文件將執行全部頂級代碼,但不會執行函數和類(由於它們只會被導入)
開
本文分享自微信公衆號 - AI科技與算法編程(kangsinx)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。