鏈接:【Python】__name__ 是什么?

來(lái)源:博客園

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

前言在我們?yōu)g覽一下 python 文件或者自己寫 python 代碼的時(shí)候,時(shí)常會(huì)在代碼的最后" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 建站知識(shí) > 【Python】__name__ 是什么?

【Python】__name__ 是什么?

時(shí)間:2023-01-31 13:28:01 | 來(lái)源:建站知識(shí)

時(shí)間:2023-01-31 13:28:01 來(lái)源:建站知識(shí)

作者:leetao

鏈接:【Python】__name__ 是什么?

來(lái)源:博客園

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。

前言

在我們?yōu)g覽一下 python 文件或者自己寫 python 代碼的時(shí)候,時(shí)常會(huì)在代碼的最后加上這樣的一行代碼

if __name__ == '__main__': func_name()那么這一行代碼有什么具體的作用呢,不加的話會(huì)對(duì)我們的結(jié)果造成影響嗎?

__name__

首先對(duì)于用雙下劃線開頭且結(jié)尾的變量,在 Python 中被稱為內(nèi)置變量,除了 __name__,我們常見(jiàn)的還有 __init__,__dict__ 等等.那么有多少內(nèi)置變量呢?我們可以通過(guò)下面在交互界面輸入下面的命令,查看 Python 全部?jī)?nèi)置變量和內(nèi)置函數(shù)

>>> dir(__builtins__)結(jié)果如下圖:





不同情況下的 __name__ 的值

首先我們需要知道 __name__ 在不同情況下會(huì)有不同值,它的值取決于我們是如何執(zhí)行腳本的.我們可以通過(guò)幾個(gè)例子感受一下:

Example 0

# test.pyprint(f'__name__ 在 test.py 值為 {__name__}')然后直接執(zhí)行一下代碼

$ python test.py然后看一下輸出

$ python test.py __name__ 在 test.py 值為 __main__在這個(gè)例子中,我們發(fā)現(xiàn) __name__ 的值是 __main__

Example 1

在這個(gè)例子中,我們重新創(chuàng)建一個(gè)腳本 test1.py 然后我們?cè)?test1.py 中調(diào)用 test.py

# test1.pyimport testprint(f'__name__ 在 test1.py 值為 {__name__}')接著執(zhí)行一下 test1.py,再看一下輸出

python test1.py __name__ 在 test.py 值為 test__name__ 在 test1.py 值為 __main__結(jié)果是不是很有意思?整個(gè)過(guò)程是什么樣子的呢?簡(jiǎn)單的畫了一個(gè)圖







什么時(shí)候使用 __name__

有時(shí)候,我們用 Python 寫了一個(gè)腳本,當(dāng)我們既希望這個(gè)腳本可以單獨(dú)運(yùn)行,同樣希望它可以在其他的腳本中發(fā)揮作用. 這個(gè)時(shí)候就需要考慮使用 __name__ 了. 這里通過(guò)改造上面 Example 1的例子來(lái)直觀感受一下

修改一下 test.py 文件

# test.pydef hello(name): print(f'Hello,{name}') if __name__ == '__main__': hello("test")再修改一下 test1.py 文件

# test1.pyfrom test import hellohello("test1")然后讓我們先嘗試直接運(yùn)行一下 test.py,很顯然這個(gè)時(shí)候, if 語(yǔ)句條件滿足,會(huì)輸出 Hello,test

$ python test.py Hello,test這個(gè)時(shí)候我們?nèi)绻\(yùn)行 test1.py,程序就會(huì)輸出 Hello,test1

$ python test1.py Hello,test1如果我們把 if __name__ == "__main__"test.py 去掉會(huì)發(fā)生什么呢?

$ python test1.py Hello,testHello,test1


Whenever the Python interpreter reads a source file, it does two things:

Let's see how this works and how it relates to your question about the __name__ checks we always see in Python scripts.

Code Sample

Let's use a slightly different code sample to explore how imports and scripts work. Suppose the following is in a file called foo.py.

# Suppose this is foo.py.print("before import")import mathprint("before functionA")def functionA(): print("Function A")print("before functionB")def functionB(): print("Function B {}".format(math.sqrt(100)))print("before __name__ guard")if __name__ == '__main__': functionA() functionB()print("after __name__ guard")

Special Variables

When the Python interpeter reads a source file, it first defines a few special variables. In this case, we care about the __name__ variable.

When Your Module Is the Main Program

If you are running your module (the source file) as the main program, e.g.

python foo.pythe interpreter will assign the hard-coded string "__main__" to the __name__ variable, i.e.

# It's as if the interpreter inserts this at the top# of your module when run as the main program.__name__ = "__main__"When Your Module Is Imported By Another

On the other hand, suppose some other module is the main program and it imports your module. This means there's a statement like this in the main program, or in some other module the main program imports:

# Suppose this is in some other main program.import fooThe interpreter will search for your foo.py file (along with searching for a few other variants), and prior to executing that module, it will assign the name "foo" from the import statement to the __name__ variable, i.e.

# It's as if the interpreter inserts this at the top# of your module when it's imported from another module.__name__ = "foo"

關(guān)鍵詞:

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉