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

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

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


当前回答

当一个 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!

其他回答

Python 主要功能是任何程序的起点. 当程序运行时,Python 翻译器连续运行代码. 主要功能只有当它作为 Python 程序运行时才运行。

def main():
    print("I am in the function")

print("I am out of the function")

当你运行脚本显示:

I am out of the function

而不是代码“我在职位”。

这是因为我们没有宣布呼叫函数“if__name__==“主”。

如果你使用它:

def main():
    print("I am in the function")

if __name__ == "__main__":
    main()

print("I am out of the function")

产量相当于

I am in the function
I am out of the function

在Python中,如果__name__==“__main__”允许您运行Python文件作为可重复使用的模块或单独的程序。

当Python 翻译器阅读源文件时,它将执行所有源文件中的代码;当Python 作为主程序运行“源文件”时,它设置了特殊变量 __name__ 以具有“__main__”值。

当您执行主函数时,它将读取如果声明,检查 __name__ 是否相当于 __main__。

如果 __name__ ==“__main__”意味着如果您正常运行 Python 文件如 python foo.py,则将特殊变量 __name__ 归分为“__main__”。

但是,如果您正在导入文件如“导入 foo”,则将 __name__ 归分为“foo”并且不会运行函数。

在 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__ 不会执行。


开发和测试您的代码

def do_important():
    """This function does something very important"""

do_important()

以此为主,以此为主,以此为主。

~$ python important.py

问题

但是,如果你想将模块导入到另一个脚本:

import important

# do_important() # I must remember to uncomment to execute this!

更好的方式

if __name__ == "__main__":
    do_important()

但是,在这一点上,有一个皮托尼的改善方式。

如果我们想从模块之外运行这个业务过程,怎么办?

如果我们把我们想要练习的代码,当我们开发并测试这样的功能时,然后立即检查“__main__”之后:

def main():
    """business logic for when running this module as the primary one!"""
    setup()
    foo = do_important()
    bar = do_even_more_important(foo)
    for baz in bar:
        do_super_important(baz)
    teardown()

# Here's our payoff idiom!
if __name__ == '__main__':
    main()

我们现在有一个最终功能,我们的模块的结束将运行,如果我们运行模块作为主模块。

它将允许模块及其功能和类被进口到其他脚本,而不运行主功能,并将允许模块(及其功能和类)在从不同的“__main__”模块运行时被召唤,即。

import important
important.main()

如果 __name__ ==“__main__”是当脚本从(说)命令线运行时运行的部分,使用像 python myscript.py 这样的命令。