这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
这是什么,为什么应该包括一个 if 声明?
if __name__ == "__main__":
print("Hello, World!")
如果你试图关闭一个问题,有人应该使用这个字符,而不是,考虑关闭作为一个复制为什么Python在我进口时运行我的模块,而我如何停止它? 问题,有人只是没有呼叫任何功能,或者错误地等待一个名为主的功能将自动作为一个输入点,使用为什么没有主()功能运行。
当前回答
如果 __name__ ==“__main__”意味着如果您正常运行 Python 文件如 python foo.py,则将特殊变量 __name__ 归分为“__main__”。
但是,如果您正在导入文件如“导入 foo”,则将 __name__ 归分为“foo”并且不会运行函数。
其他回答
您可以通过这个简单的例子查看特殊变量 __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
接收“ab.py”文件:
def a():
print('A function in ab file');
a()
import ab
def main():
print('main function: this is where the action is')
def x():
print ('peripheral task: might be useful in other projects')
x()
if __name__ == "__main__":
main()
当您执行 xy.py 时,您将导入 ab. 导入声明立即在导入上运行模块,因此 ab 的操作在 xy 的剩余之前进行。
当你运行一个脚本 - 无论你命名什么 - 翻译者称之为“__main__”,使它成为主或“家”脚本,在运行一个外部脚本后返回。
下面的两行意味着:“如果这是“__main__”或“home”脚本,执行称为main()的函数”。这就是为什么你会看到一个 def main():锁上顶部,其中包含脚本的功能的主要流。
為什麼要實施這一點?
但代码在没有它的情况下工作。
假设我在维基百科上写了网页扫描的功能和课程,当然,这可能不是一个好例子。
我想在另一个程序中使用这些功能,而不重写它。
好吧,我进口它们,但在该文件的结尾我把 __name__ == '__main__'
当我们进口一个模块时,它内部的所有代码都从开始到结束,但当它达到条件时,它不运行函数、函数2等,即维基百科 __scrape__。
好吧,在全球范围内,Python __name__ 为当前程序定义为“__main__”。
当我们进口一个模块时,它被定义为我们当前程序的名称空间中的变量,而当前程序 __name__ 是“__main__”:
文件测试.py
def func():
# Do something
pass
def func2():
# Do something
pass
print('The program name is set to ', globals()['__name__'])
if __name__=='__main__':
# In the current program, __name__ is equal to '__main__'
func('https://www.wikipedia.org')
func2('https://www.wikipedia.org')
# Or do more jobs
import test1
print('inside of current program')
print('name is current program', __name__)
print(globals()['test1'])
test1.func('another site')
test1.func2('another site')
出口
inside of test 1
name of program is set to test1
end of module
inside of current
__main__
<module 'test1' from 'C:\\users\\ir\\appdata\\local\\programs\\python\\python38\\lib\\test1.py'>
考虑一下:
print __name__
上面的结果是 __main__。
if __name__ == "__main__":
print "direct method"
上述声明是真实的,并打印“直接方法”。假设如果他们进口这个类别到另一个类别,它不会打印“直接方法”,因为在进口时,它将设置 __name__ 等于“第一型号名称”。
当您的脚本运行时,将其作为命令传递给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