_名称_变量及其在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/