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

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

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


当前回答

例如,这是 test1.py 没有如果 __name__ ==“__main__”::

# "test1.py"

def hello()
    print("Hello")

hello()

而且 test2.py 只輸入 test1.py:

# "test2.py"

import test1 # Here


然后,当运行 test2.py 时,一个 Hello 被打印,因为 test1.py 中的不需要的 hello() 代码也被运行:

python test2.py
Hello

当然,您可以在 test2.py 中拨打 test1.hello():

# "test2.py"

import test1

test1.hello() # Here

然后,当运行测试2时,印刷了两个Hello:

python test2.py
Hello
Hello

现在,添加如果 __name__ ==“__main__”:测试1.py 并将 hello() 放在下面:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()

此分類上一篇: Test2.py:

# "test2.py"

import test1

test1.hello()

然后,在运行 test2.py 时,只有一个 Hello 被打印,因为如果 __name__ ==“__main__”:在 test2.py 输入 test1.py 时阻止运行不需要的 hello() 代码:

python test2.py
Hello

此外, test1.py 是否有 __name__ == “__main__”: 或者不:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()
# "test1.py"

def hello()
    print("Hello")

hello()

One Hello 在运行 test1.py 时正确打印:

python test1.py
Hello

其他回答

简单的说法:

下面看到的代码如果 __name__ ==“__main__”:只有当您的 Python 文件作为 python 示例1.py 执行时才会被召回。

但是,如果您想将您的 Python 文件 example1.py 作为一个模块来与另一个 Python 文件一起工作,请说 example2.py,下面的代码如果 __name__ ==“__main__”:不会运行或采取任何效果。

当您互动运行Python时,本地 __name__ 变量被分配为 __main__ 的值。 同样,当您从命令行中执行一个Python 模块时,而不是将其导入到另一个模块时,其 __name__ 属性被分配为 __main__ 的值,而不是模块的实际名称。

if __name__ == '__main__':
    # Do something appropriate here, like calling a
    # main() function defined elsewhere in this module.
    main()
else:
    # Do nothing. This module has been imported by another
    # module that wants to make use of the functions,
    # classes and other useful bits it has defined.

如果 __name__ ==“__main__”:基本上是顶级脚本环境,并指定翻译者(‘我首先要执行的优先事项’)。

“__main__”是顶级代码执行范围的名称,当从标准输入、脚本或互动快点阅读时,模块的 __name__ 设置相当于“__main__”。

if __name__ == "__main__":
    # Execute only if run as a script
    main()

# XXX FIXME: useless (see below)
if __name__ == "__main__":
    n = int(input('Write a number: '))
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    print('Fibonacci number %i: %i' % (n, b))

n = int(input('Write a number: '))
a, b = 0, 1
while b < n:
    a, b = b, a+b
print('Fibonacci number %i: %i' % (n, b))

如果你想能够进口纤维,第一个版本是无用的,也是,因为有用的代码在一个部分,它不会运行,当你进口这个文件(在这种情况下 __name__ 不会是“__main__”). 正确的设计在这种情况下将是恢复代码,以便有用的部分在一个功能,你可以运行,当你想要之后你已经进口它。

def main():
    n = int(input('Write a number: '))
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    print('Fibonacci number %i: %i' % (n, b))

if __name__ == "__main__":
    main()

事实上,更好的设计仍然是将可重复使用的部分(实际计算)与用户可见的输入/输出隔离:

def fibn(n: int) -> int:
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    return b

def main() -> None:
    n = int(input('Write a number: '))
    print('Fibonacci number %i: %i' % (n, fibn(n)))

if __name__ == "__main__":
    main()

现在,您可以从 Fib 进口 Fibn 并从执行此进口的代码呼叫 Fibn() 函数。

def main():
    lock = thread.allocate_lock()
    thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
    thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

if __name__ == "__main__":
    main()

(不同于类似于C的语言,主要名称对Python没有具体的含义;但它是常见的公约使用它作为将运行的东西的名称。

简单地说,这是运行文件的输入点,就像C编程语言的主要功能一样。