(Python的)__ name__中包含什么?
_名稱_變量及其在Python中的用法簡(jiǎn)介 (An introduction to the _ _name_ _ variable and its usage in Python)
You’ve most likely seen the __name__ variable when you’ve gone through Python code. Below you see an example code snippet of how it may look:
通過(guò)Python代碼,您最有可能看到了__name__變量。 在下面,您可以查看其外觀的示例代碼片段:
if __name__ == '__main__': main()In this article, I want to show you how you can make use of this variable to create modules in Python.
在本文中,我想向您展示如何利用此變量在Python中創(chuàng)建模塊。
為什么使用_名稱_變量? (Why is the _ _name_ _ variable used?)
The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script.
__name__變量(前后兩個(gè)下劃線)是一個(gè)特殊的Python變量。 它的取值取決于我們執(zhí)行包含腳本的方式。
Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.
有時(shí),您編寫的腳本所包含的功能可能在其他腳本中也很有用。 在Python中,您可以將該腳本作為另一個(gè)腳本中的模塊導(dǎo)入。
Thanks to this special variable, you can decide whether you want to run the script. Or that you want to import the functions defined in the script.
借助此特殊變量,您可以決定是否要運(yùn)行腳本。 或者您要導(dǎo)入腳本中定義的功能。
__name__變量可以包含哪些值? (What values can the __name__ variable contain?)
When you run your script, the __name__ variable equals __main__. When you import the containing script, it will contain the name of the script.
運(yùn)行腳本時(shí), __name__變量等于__main__ 。 導(dǎo)入包含腳本時(shí),它將包含腳本的名稱。
Let us take a look at these two use cases and describe the process with two illustrations.
讓我們看一下這兩個(gè)用例,并用兩個(gè)插圖描述該過(guò)程。
方案1-運(yùn)行腳本 (Scenario 1 - Run the script)
Suppose we wrote the script nameScript.py as follows:
假設(shè)我們編寫了腳本 nameScript.py如下:
def myFunction(): print 'The value of __name__ is ' + __name__def main(): myFunction()if __name__ == '__main__': main()If you run nameScript.py, the process below is followed.
如果運(yùn)行nameScript.py,則遵循以下過(guò)程。
Before all other code is run, the __name__ variable is set to __main__. After that, the main and myFunction def statements are run. Because the condition evaluates to true, the main function is called. This, in turn, calls myFunction. This prints out the value of __main__.
在運(yùn)行所有其他代碼之前,將__name__變量設(shè)置為_(kāi)_main__。 之后, main 和myFunction def語(yǔ)句運(yùn)行。 因?yàn)闂l件的計(jì)算結(jié)果為true,所以將調(diào)用main函數(shù)。 依次調(diào)用myFunction。 這將打印出__main__的值。
方案2-將腳本導(dǎo)入另一個(gè)腳本 (Scenario 2 - Import the script in another script)
If we want to re-use myFunction in another script, for example importingScript.py, we can import nameScript.py as a module.
如果要在另一個(gè)腳本中重新使用myFunction,例如importingScript.py ,則可以將nameScript.py導(dǎo)入為模塊。
The code in importingScript.py could be as follows:
importingScript.py的代碼可能如下:
import nameScript as nsns.myFunction()We then have two scopes: one of importingScript and the second scope of nameScript. In the illustration, you’ll see how it differs from the first use case.
然后,我們有兩個(gè)作用域:一個(gè)是importingScript ,另一個(gè)是nameScript 。 在圖中,您將看到它與第一個(gè)用例有何不同。
In importingScript.py the __name__ variable is set to __main__. By importing nameScript, Python starts looking for a file by adding .py to the module name. It then runs the code contained in the imported file.
在importingScript.py中, __name__變量設(shè)置為_(kāi)_main__。 通過(guò)導(dǎo)入nameScript,Python通過(guò)在模塊名稱中添加.py開(kāi)始查找文件。 然后,它將運(yùn)行導(dǎo)入文件中包含的代碼。
But this time it is set to nameScript. Again the def statements for main and myFunction are run. But, now the condition evaluates to false and main is not called.
但是這次 設(shè)置為nameScript。 再次運(yùn)行main和myFunction的def語(yǔ)句。 但是,現(xiàn)在條件評(píng)估為false且main不被調(diào)用。
In importingScript.py we call myFunction which outputs nameScript. NameScript is known to myFunction when that function was defined.
在importingScript.py中,我們調(diào)用myFunction來(lái)輸出nameScript。 定義函數(shù)時(shí),myFunction知道NameScript。
If you would print __name__ in the importingScript, this would output __main__. The reason for this is that Python uses the value known in the scope of importingScript.
如果要在importingScript中打印__name__ ,則將輸出__main__ 。 原因是Python使用importingScript范圍內(nèi)的已知值。
結(jié)論 (Conclusion)
In this short article, I explained how you can use the __name__ variable to write modules. You can also run these modules on their own. This can be done by making use of how the values of these variables change depending on where they occur.
在這篇簡(jiǎn)短的文章中,我解釋了如何使用__name__變量編寫模塊。 您也可以單獨(dú)運(yùn)行這些模塊。 這可以通過(guò)利用這些變量的值根據(jù)它們發(fā)生的位置如何變化來(lái)完成。
翻譯自: https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/
總結(jié)
以上是生活随笔為你收集整理的(Python的)__ name__中包含什么?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到很多鸽子飞预示什么
- 下一篇: 前端速成班_在此速成班中学习Go