这样做做什么,为什么应该包括:if语句?

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

如果你试图结束一个问题 如果你想要结束一个问题 在那里有人应该应该 使用这个学说,而不是,是不是, 考虑关闭作为 重复的为什么Python在我进口时 运行我的模块? 我该如何阻止它?。而对于某些问题,如果有人只是没有调用任何函数,或者错误地期望指定函数main自动用作切入点,使用当我启动 Python 脚本时, 主函数为何不运行? 脚本从哪里开始运行 ?.


当前回答

简单的说,它是一个切入点 运行文件,像main函数中C编程语言。

其他回答

考虑:

print __name__

上述产出是__main__.

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

上述陈述属实,印有指纹“直接方法”假想他们是否将这门课 输入到另一门课上,它不会打印“直接方法”因为在进口时,它将设置__name__ equal to "first model name".

简言之,__name__是为每个脚本定义的变量,以定义脚本是作为主模块运行还是作为导入模块运行。

如果我们有两本脚本,

#script1.py
print "Script 1's name: {}".format(__name__)

#script2.py
import script1
print "Script 2's name: {}".format(__name__)

执行脚本1 的输出为

Script 1's name: __main__

执行脚本2 的输出为 :

Script1's name is script1
Script 2's name: __main__

如你所见__name__告诉我们哪个代码是“ main” 模块。 这很棒, 因为您可以只写代码, 而不必担心 C/ C++ 中的结构问题, 如果一个文件不执行“ main” 函数, 那么它无法编译为可执行文件, 如果它可以, 那么它就不能用作图书馆 。

说您会写出一个 Python 脚本,该脚本能做一些伟大的事情,而您会执行一系列用于其他目的的功能。如果我想使用这些功能,我可以在不执行您的程序的情况下导入并使用您的脚本(因为您的代码仅在if __name__ == "__main__":在 C/C++ 中,您需要将这些部分分割成一个单独的模块, 包括文件。 图片如下:

Complicated importing in C

箭头是导入链接。 对于试图包含前一个模块代码的3个模块,每个模块有6个文件(9个,计算执行文件)和5个链接。这使得将其他代码纳入 C 工程很难,除非它是专门编译成一个库。 现在为 Python 绘制它:

Elegant importing in Python

你写一个模块, 如果有人想使用你的代码, 他们只是导入它和__name__变量可以帮助将程序的可执行部分与库部分分开。

用于从命令行调用 Python 文件。 它通常用于调用“ main ()) ” 函数或执行其他合适的启动代码, 比如, 处理命令行参数 。

也可以以几种方式写成。

def some_function_for_instance_main():
    dosomething()


__name__ == '__main__' and some_function_for_instance_main()

我不是说你应该用这个来做生产代码, 但是它可以说明,没有什么"神奇"if __name__ == '__main__'.

这只是援引 Python 文件中主要功能的常规 。

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

Output

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

现在假设我们从另一个剧本中 将上述剧本称为:

脚本外部调用(_P).py

import test

print(test.apple)
test.hello_world()

print(test.__name__)

当你执行此操作时,

python external_calling.py

Output

42
I am inside hello_world
test

因此,以上不言自明地说明,当你们呼唤测试测试从另一个脚本, 如果循环__name__test.py将不执行。

假设我在维基百科上为网上剪贴写功能和课程。 当然,这也许不是一个好例子。

我想在另一个程序里使用这些函数 而不改写它

嗯,我进口了它们, 但是在文件的结尾,我放__name__ == '__main__'

当我们importa 模块,其中的所有代码都从开始到结束执行。但是当它达到条件时,它不会运行调乐曲 func, 复真2c2cfluc2等,这是维基百科__scrape__.

那么,在全球范围内,Python(Python)__name__定义为'__main__'用于当前程序 。

当我们import模块,它定义为当前程序和当前程序的名称空间中的变量。__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')

Output

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'>