作者:leetao
链接:【Python】__name__ 是什么?
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
前言
在我们浏览一下 python 文件或者自己写 python 代码的时候,时常会在代码的最后加上这样的一行代码
if __name__ == '__main__':
func_name()
那么这一行代码有什么具体的作用呢,不加的话会对我们的结果造成影响吗?
__name__
首先对于用双下划线开头且结尾的变量,在 Python 中被称为内置变量,除了 __name__
,我们常见的还有 __init__
,__dict__
等等.那么有多少内置变量呢?我们可以通过下面在交互界面输入下面的命令,查看 Python 全部内置变量和内置函数
>>> dir(__builtins__)
结果如下图:
不同情况下的 __name__
的值
首先我们需要知道 __name__
在不同情况下会有不同值,它的值取决于我们是如何执行脚本的.我们可以通过几个例子感受一下:
Example 0
# test.py
print(f'__name__ 在 test.py 值为 {__name__}')
然后直接执行一下代码
$ python test.py
然后看一下输出
$ python test.py
__name__ 在 test.py 值为 __main__
在这个例子中,我们发现 __name__
的值是 __main__
Example 1
在这个例子中,我们重新创建一个脚本 test1.py 然后我们在 test1.py 中调用 test.py
# test1.py
import test
print(f'__name__ 在 test1.py 值为 {__name__}')
接着执行一下 test1.py,再看一下输出
python test1.py
__name__ 在 test.py 值为 test
__name__ 在 test1.py 值为 __main__
结果是不是很有意思?整个过程是什么样子的呢?简单的画了一个图
什么时候使用 __name__
有时候,我们用 Python 写了一个脚本,当我们既希望这个脚本可以单独运行,同样希望它可以在其他的脚本中发挥作用. 这个时候就需要考虑使用 __name__
了. 这里通过改造上面 Example 1的例子来直观感受一下
修改一下 test.py 文件
# test.py
def hello(name):
print(f'Hello,{name}')
if __name__ == '__main__':
hello("test")
再修改一下 test1.py 文件
# test1.py
from test import hello
hello("test1")
然后让我们先尝试直接运行一下 test.py
,很显然这个时候, if 语句条件满足,会输出 Hello,test
$ python test.py
Hello,test
这个时候我们如果运行 test1.py
,程序就会输出 Hello,test1 了
$ python test1.py
Hello,test1
如果我们把 if __name__ == "__main__"
在 test.py
去掉会发生什么呢?
$ python test1.py
Hello,test
Hello,test1
Whenever the Python interpreter reads a source file, it does two things:
- it sets a few special variables like
__name__
, and then - it executes all of the code found in the file.
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 math
print("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.py
the 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 foo
The 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"