_名稱_變量及其在Python中的用法簡介 (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:
通過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中創建模塊。
為什么使用_名稱_變量? (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__
變量(前后兩個下劃線)是一個特殊的Python變量。 它的取值取決于我們執行包含腳本的方式。
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.
有時,您編寫的腳本所包含的功能可能在其他腳本中也很有用。 在Python中,您可以將該腳本作為另一個腳本中的模塊導入。
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.
借助此特殊變量,您可以決定是否要運行腳本。 或者您要導入腳本中定義的功能。
__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.
運行腳本時, __name__
變量等于__main__
。 導入包含腳本時,它將包含腳本的名稱。
Let us take a look at these two use cases and describe the process with two illustrations.
讓我們看一下這兩個用例,并用兩個插圖描述該過程。
方案1-運行腳本 (Scenario 1 - Run the script)
Suppose we wrote the script nameScript.py
as follows:
假設我們編寫了腳本 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.
如果運行nameScript.py,則遵循以下過程。
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__
.
在運行所有其他代碼之前,將__name__
變量設置為__main__。 之后, main
和myFunction
def語句運行。 因為條件的計算結果為true,所以將調用main函數。 依次調用myFunction。 這將打印出__main__
的值。
方案2-將腳本導入另一個腳本 (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.
如果要在另一個腳本中重新使用myFunction,例如importingScript.py
,則可以將nameScript.py
導入為模塊。
The code in importingScript.py
could be as follows:
importingScript.py
的代碼可能如下:
import nameScript as ns
ns.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.
然后,我們有兩個作用域:一個是importingScript
,另一個是nameScript
。 在圖中,您將看到它與第一個用例有何不同。
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__
變量設置為__main__。 通過導入nameScript,Python通過在模塊名稱中添加.py
開始查找文件。 然后,它將運行導入文件中包含的代碼。
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.
但是這次 設置為nameScript。 再次運行main和myFunction的def語句。 但是,現在條件評估為false且main不被調用。
In importingScript.py we call myFunction which outputs nameScript. NameScript is known to myFunction when that function was defined.
在importingScript.py中,我們調用myFunction來輸出nameScript。 定義函數時,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范圍內的已知值。
結論 (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.
在這篇簡短的文章中,我解釋了如何使用__name__
變量編寫模塊。 您也可以單獨運行這些模塊。 這可以通過利用這些變量的值根據它們發生的位置如何變化來完成。
翻譯自: https://www.freecodecamp.org/news/whats-in-a-python-s-name-506262fe61e8/