这样做做什么,为什么应该包括:if语句?

if __name__ == "__main__":
    print("Hello, World!")

如果你试图结束一个问题 如果你想要结束一个问题 在那里有人应该应该 使用这个学说,而不是,是不是, 考虑关闭作为 重复的为什么Python在我进口时 运行我的模块? 我该如何阻止它?。而对于某些问题,如果有人只是没有调用任何函数,或者错误地期望指定函数main自动用作切入点,使用当我启动 Python 脚本时, 主函数为何不运行? 脚本从哪里开始运行 ?.


当前回答

执行模块(源文件)时如果检查模块是直接被调用还是从其它源文件调用。 这里直接表示未导入 。

如果直接被叫执行,模块名称将设置为“main”,然后在如果块将被执行。

其他回答

Python 中每个模块的属性都称为属性__name__的值。__name__属性是__main__当模块直接运行时, 例如python my_module.py否则(就像你说的那样)import my_module)值的数值__name__是模块的名称。

简而言之,仅举一个小例子来解释。

脚本测试.py

apple = 42

def hello_world():
    print("I am inside hello_world")

if __name__ == "__main__":
    print("Value of __name__ is: ", __name__)
    print("Going to call hello_world")
    hello_world()

我们可以直接执行

python test.py

Output

Value of __name__ is: __main__
Going to call hello_world
I am inside hello_world

现在假设我们从另一个剧本中 将上述剧本称为:

脚本外部调用(_P).py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

当你执行此操作时,

python external_calling.py

Output

42
I am inside hello_world
test

因此,以上不言自明地说明,当你们呼唤测试测试从另一个脚本, 如果循环__name__test.py将不执行。

简言之,__name__是为每个脚本定义的变量,以定义脚本是作为主模块运行还是作为导入模块运行。

如果我们有两本脚本,

#script1.py
print "Script 1's name: {}".format(__name__)

#script2.py
import script1
print "Script 2's name: {}".format(__name__)

执行脚本1 的输出为

Script 1's name: __main__

执行脚本2 的输出为 :

Script1's name is script1
Script 2's name: __main__

如你所见__name__告诉我们哪个代码是“ main” 模块。 这很棒, 因为您可以只写代码, 而不必担心 C/ C++ 中的结构问题, 如果一个文件不执行“ main” 函数, 那么它无法编译为可执行文件, 如果它可以, 那么它就不能用作图书馆 。

说您会写出一个 Python 脚本,该脚本能做一些伟大的事情,而您会执行一系列用于其他目的的功能。如果我想使用这些功能,我可以在不执行您的程序的情况下导入并使用您的脚本(因为您的代码仅在if __name__ == "__main__":在 C/C++ 中,您需要将这些部分分割成一个单独的模块, 包括文件。 图片如下:

Complicated importing in C

箭头是导入链接。 对于试图包含前一个模块代码的3个模块,每个模块有6个文件(9个,计算执行文件)和5个链接。这使得将其他代码纳入 C 工程很难,除非它是专门编译成一个库。 现在为 Python 绘制它:

Elegant importing in Python

你写一个模块, 如果有人想使用你的代码, 他们只是导入它和__name__变量可以帮助将程序的可执行部分与库部分分开。

创建以下两个文件 :

# a.py

import b
# b.py

print("__name__ equals " + __name__)

if __name__ == '__main__':
    print("if-statement was executed")

现在每个文件都单独运行 。


运行中python a.py:

$ python a.py
__name__ equals b

何时a.py执行时,它导入模块b。这导致所有代码都在里面b要运行。 Python 设置globals()['__name__']和在b模块的模块到模块名称,b.


运行中python b.py:

$ python b.py
__name__ equals __main__
if-statement was executed

仅在文件b.py已执行, Python 设置globals()['__name__']在此文件中的文件中"__main__"因此,if用于True这一次。

当我们的模块中有某些语句时(M.py)我们想要当它作为主(而非导入)运行时被执行, 我们可以将这些语句( 测试案例、 打印语句) 置于此下if区块。

默认值( 当模块作为主模式运行时, 不导入)__name__变量设置为"__main__",当它将进口__name__变量将获得不同的值, 最有可能是模块的名称('M'这有助于将模块的不同变量合并运行,并区分其具体的输入和输出语句,以及如果有任何测试案例的话。

简简,使用此“if __name__ == "main"防止模块导入时运行(确定)代码的块。

您可以查看特殊变量__name__使用此简单示例 :

创建创建文件1. py

if __name__ == "__main__":
    print("file1 is being run directly")
else:
    print("file1 is being imported")

创建 *file2。平平

import file1 as f1

print("__name__ from file1: {}".format(f1.__name__))
print("__name__ from file2: {}".format(__name__))

if __name__ == "__main__":
    print("file2 is being run directly")
else:
    print("file2 is being imported")

执行文件2. py

Output:

file1 is being imported
__name__ from file1: file1
__name__ from file2: __main__
file2 is being run directly