深入理解python中if?__name__?==?‘__main__‘
1. 一個簡單的例子
先來看一份代碼:
# test.py
def addFunc(a, b):
return a + b
print('the result of test is: 1 + 1 = ', addFunc(1, 1))# mode.py
import test
print('The result of test modula is :', test.addFunc(12, 23))執(zhí)行 mode.py 輸出如下:
the result of test is: 1 + 1 = 2
The result of test modula is : 35
這里輸出的語句中,同時包含了 test.py 和 mode.py 中的內(nèi)容。那么問題來了,很多時候調(diào)用者并不需要輸出 test.py 里面的內(nèi)容,這個時候應該怎么處理呢?添加另外一個不輸出的版本 test_no_print.py ?
python 中提供了 __name__ 這個系統(tǒng)變量來解決這個問題,先看修改后的代碼:
# test.py
def addFunc(a, b):
return a + b
if __name__ == '__main__':
print('the result of test is: 1 + 1 = ', addFunc(1, 1))單獨執(zhí)行 test.py 結(jié)果如下:
the result of test is: 1 + 1 = 2
沒有問題,這是我們想要的結(jié)果。
執(zhí)行 mode.py:
The result of test modula is : 35
這恰恰也是我們想要的結(jié)果。
那么問題來了,__name__ 里面究竟是個什么神奇的值?
2. __name__ 的值
修改 test.py 如下使其輸出 __name__ 的值:
# test.py
def addFunc(a, b):
return a + b
if __name__ == '__main__':
print('the result of test is: 1 + 1 = ', addFunc(1, 1))
print("The value of '__name__' is ", __name__)分別執(zhí)行 test.py 和 mode.py 結(jié)果如下:
the result of test is: 1 + 1 = 2
The value of '__name__' is __main__
The value of '__name__' is test
The result of test modula is : 35
可以看到,單獨執(zhí)行 test.py 時,__name__ 的值是 ‘__main__’,而作為模塊被 import 到其他文件中調(diào)用時,__name__ 的值則是模塊的名字。
我們知道,有兩種方法可以使用 python 文件來執(zhí)行它實現(xiàn)的功能,一個是直接運行,一個是被其他文件導入后調(diào)用。當它被直接運行時,當前文件就是程序的主入口,這相當于 C 或者 Java 中的 main 函數(shù)。當它被其他文件導入調(diào)用時,程序的入口自然在其他文件中。
__name__ 是 python 的內(nèi)置屬性,這個系統(tǒng)全局變量用來表示當前模塊的名字。而 __main__ 就是一個代表程序入口的字符串。 因此 if __name__ == ‘__main__’ 其實就是判斷程序的入口是不是當前的文件!
到此這篇關(guān)于深入理解python中if __name__ == ‘__main__‘的文章就介紹到這了,更多相關(guān)python if __name__ == ‘__main__‘內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中if __name__ == "__main__"詳細解釋
- 正確理解Python中if __name__ == ''__main__''
- Python中if?__name__==‘__main__‘用法詳情
- Python中if __name__ == ''__main__''作用解析
- 聊聊Python代碼中if?__name__?==?‘__main__‘的作用是什么
- 關(guān)于Python中的if __name__ == __main__詳情
- 一篇文章徹底弄懂Python中的if?__name__?==?__main__
- Python中if __name__ == “__main__“的作用總結(jié)
相關(guān)文章
如何使用?Python?實現(xiàn)?DeepSeek?R1?本地化部署
文章介紹了如何使用Python實現(xiàn)DeepSeekR1本地化部署,包括硬件環(huán)境、Python環(huán)境、安裝依賴包、配置與運行代碼等步驟,幫助讀者輕松部署并運行本地AI助手,感興趣的朋友一起看看吧2025-02-02
python面向?qū)ο缶幊淘O(shè)計原則之單一職責原則詳解
這篇文章主要為大家詳細介紹了python面向?qū)ο缶幊淘O(shè)計原則之單一職責原則,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
pytorch?dataset實戰(zhàn)案例之讀取數(shù)據(jù)集的代碼
這篇文章主要介紹了pytorch?dataset實戰(zhàn)案例讀取數(shù)據(jù)集的相關(guān)知識,這段代碼可以作為讀取數(shù)據(jù)集的一個DataSet類的基礎(chǔ)類,可以擴充進行修改,以后有類似需要可以拿過來修改,本文結(jié)合實例代碼給大家詳細講解,需要的朋友可以參考下2022-10-10
Python圖像處理庫PIL的ImageGrab模塊介紹詳解
這篇文章主要介紹了Python圖像處理庫PIL的ImageGrab模塊介紹詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02
Android 兼容性問題:java.lang.UnsupportedOperationException解決辦法
這篇文章主要介紹了Android 兼容性問題:java.lang.UnsupportedOperationException解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Python Tornado之跨域請求與Options請求方式
這篇文章主要介紹了Python Tornado之跨域請求與Options請求方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

