这是什么,为什么应该包括一个 if 声明?

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

如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。


当前回答

您可以通过这个简单的例子查看特殊变量 __name__:

创建 file1.py

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

创建 *file2.py

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")

执行 file2.py

出口:

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

其他回答

当您的脚本运行时,将其作为命令传递给Python翻译器时,

python myscript.py

定义的函数和类是定义的,好,但它们的代码没有运行。 与其他语言不同,没有主要()函数会自动运行 - 主要()函数是暗示所有代码在顶级。

在这种情况下,顶级代码是如果区块。 __name__ 是一个内置变量,以当前模块的名称进行评估. 但是,如果模块正在直接运行(如 myscript.py 上),则 __name__ 取代设置为“__main__”字符串。

if __name__ == "__main__":
    ...

如果您的脚本被导入到另一个模块,其各种功能和类定义将被导入,其顶级代码将被执行,但如果上述条款的后代代码不会运行,因为条件不满足。

# file one.py
def func():
    print("func() in one.py")

print("top-level in one.py")

if __name__ == "__main__":
    print("one.py is being run directly")
else:
    print("one.py is being imported into another module")
# file two.py
import one

print("top-level in two.py")
one.func()

if __name__ == "__main__":
    print("two.py is being run directly")
else:
    print("two.py is being imported into another module")

python one.py

产量将是

top-level in one.py
one.py is being run directly

如果您运行 two.py 而不是:

python two.py

你得到

top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly

如果此.py 文件是由其他.py 文件进口的,则如果声明下的代码将不会被执行。

如果这个.py 是由 python 运行这个_py.py 在 Shell 下, 或在 Windows 中双击, 该代码在 if 声明 下将执行。

通常是用来测试的。

考虑一下:

print __name__

上面的结果是 __main__。

if __name__ == "__main__":
  print "direct method"

上述声明是真实的,并打印“直接方法”。假设如果他们进口这个类别到另一个类别,它不会打印“直接方法”,因为在进口时,它将设置 __name__ 等于“第一型号名称”。

在 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

出口

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

现在假设我们从另一个脚本中称之为上面的脚本:

编辑 external_calling.py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

当你执行这一点时,

python external_calling.py

出口

42
I am inside hello_world
test

因此,上述是自我解释的,当你从另一个脚本打电话测试时,如果在 test.py 中的 loop __name__ 不会执行。

当一个 Python 文件运行时,它创建了许多特殊变量,如 __name__. 变量 __name__ 保留了文件的名称。

if __name__ == "__main__":
       # Do something

这意味着,如果执行的文件的名称运行为源文件而不是模块,那么它将运行代码在其中。 这可以通过一个简单的例子证明。 创建两个 Python 文件, foo.py 和 second.py. 然后在 foo.py 中输入此:

if __name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

在第二.py 中,输入此:

import foo

if foo.__name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

除此之外,如果你要做这个打印(__name__),那么它会打印 __main__。

因为文件是作为主要来源运行,如果你打印(foo.__name__)它将打印 foo 因为默认值的 __name__ 变量是文件的名称,默认情况下我意味着你也可以更改它。

__name__ = "Hello, World!"
print(__name__)

然后,产量将是:

Hello, World!