这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
只有你需要知道的事实
实际的机械是相当简单的,只有两个基本事实:
纯 Python 模块总是用“__main__”字符串设置的变量 __name__ 创建。 导入一个模块具有改变 __name__ 变量的副作用,而无需.py 扩展。
如何使用此信息
人們寫 __name__ == '__main__' 以測試是否有一個模組已被輸入。
通常有某些代码不应该在进口时运行:测试代码,一次使用代码,命令线前端或网页服务器前端。
正如你可以从其他答案中看到的那样,人们似乎能够无限地谈论这个主题,但它确实是一个简单的事情,并且很容易掌握。
其他回答
如果 Python 翻译器运行一个特定的模块,那么 __name__ 全球变量将有“__main__”的值:
def a():
print("a")
def b():
print("b")
if __name__ == "__main__":
print ("you can see me")
a()
else:
print ("You can't see me")
b()
当你运行这个脚本时,它打印:
you can see me
a
如果您输入此文件,请说 A 到 B 文件,然后执行 B 文件,然后如果 __name__ == “__main__” 在 A 文件中变成错误,那么它打印:
You can't see me
b
短答
它是字符串代码,保护用户在他们不打算时偶然提起脚本,这里有一些常见问题,当警卫被忽略了一个脚本:
长答
每当Python翻译器阅读源文件时,它都会做两件事:
让我们看看它是如何工作的,以及它是如何与您的问题有关的 __name__ 检查我们总是看到在 Python 脚本。
让我们使用一个稍微不同的代码样本来探索进口和脚本是如何工作的。
# Suppose this is foo.py.
print("before import")
import math
print("before function_a")
def function_a():
print("Function A")
print("before function_b")
def function_b():
print("Function B {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
print("after __name__ guard")
当您的模块是主要程序时
python foo.py
# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__"
当您的模块由另一个模块进口时
# Suppose this is in some other main program.
import foo
# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"
# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
如果您的模块是主要程序,那么它会看到 __name__ 实际上设置为“__main__”并称为两个功能,打印线“函数 A”和“函数 B 10.0”。
只有当您的模块被另一个模块进口时
总是
它将在两种情况下打印“ __name__ 警卫”字符串。
总结
# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard
為什麼它會這樣工作?
# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo2 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
if __name__ == "__main__":
print("m1")
function_a()
print("m2")
print("t2")
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters
def function_a():
print("a1")
from foo3 import function_b
print("a2")
function_b()
print("a3")
def function_b():
print("b")
print("t1")
print("m1")
function_a()
print("m2")
print("t2")
# Suppose this is in foo4.py
__name__ = "__main__"
def bar():
print("bar")
print("before __name__ guard")
if __name__ == "__main__":
bar()
print("after __name__ guard")
当您执行模块(源文件)时,条件是否检查模块是否直接被召唤或从另一个源文件被召唤。
如果直接呼吁执行,则模块名称将设置为“主”,然后在如果区块内的代码将执行。
这个答案是Java程序员学习Python. 每个Java文件通常包含一个公共类。
从其他文件中呼叫课堂. 你只需要将其进口到呼叫程序. 单独运行课堂站,用于测试目的。
对于后者来说,该类应该包含一个公共静态虚假主要()方法,在Python中,这个目的由全球定义的标签“__main__”提供。
您可以将文件作为一个脚本以及一个可进口的模块使用。
fibo.py(一个名为fibo的模块)
# Other modules can IMPORT this MODULE to use the function fib
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a+b
print()
# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
参考: https://docs.python.org/3.5/tutorial/modules.html