Python關(guān)于 if __name__ == "__main__"

Following words copyed from Python documentation

__name__

The __name__ attribute must be set to the fully-qualified name of the module. This name is used to uniquely identify the module in the import system.

__main__

__main__ is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:

if __name__ == "__main__":
# execute only if run as a script
main()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,436評(píng)論 0 10
  • 今天,我把花盆挖了個(gè)洞,再把蘋果種子種在花盆里,并埋住。 每...
    李雅康閱讀 375評(píng)論 0 0
  • 看完這些也就算是可以玩開發(fā)了 1os是管理和控制計(jì)算機(jī)軟件和硬件資源的計(jì)算機(jī)程序;是直接運(yùn)行在裸機(jī)上的系統(tǒng)軟件;任...
    資深程序猿閱讀 429評(píng)論 0 0
  • 夏天是一個(gè)非常適合擼串的季節(jié),從南到北,從東到西,不同的地方都有自己當(dāng)?shù)氐奶厣瓜瑪]串只是夜宵的一種,但是深得男...
    韻月思密達(dá)閱讀 515評(píng)論 2 1