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

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

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


当前回答

当你import具有此条件的代码, 它会返回False(除进口编码外),但将返回True将要运行的代码的代码 。

其他回答

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

也可以以几种方式写成。

def some_function_for_instance_main():
    dosomething()


__name__ == '__main__' and some_function_for_instance_main()

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

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

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

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

嗯,我进口了它们, 但是在文件的结尾,我放__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'>

当您在本地交互运行 Python 时__name__变量的变量被指定值__main__同样,当您从命令行执行 Python 模块时,而不是将它导入到另一个模块中时,它__name__属性的属性被指定值__main__,而不是模块的实际名称。这样,模块可以查看自己的__name__用于确定它们是如何使用的值, 无论是作为对其它程序的支持还是作为从命令行执行的主要应用程序。 因此, 在 Python 模块中, 下列语系非常常见 :

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.

if __name__ == "__main__":基本上是顶层的脚本环境, 它指定了翻译( “ 我最优先首先执行 ” ) 。

'__main__'是顶层代码执行范围的名称。模块的__name__设置为等于'__main__'从标准输入、脚本或交互式提示读取。

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

简而言之,你需要知道几点:

  1. import a动作实际上运行所有可以运行的a.py,意指每一行a.py

  2. 因为第1点,你可能不希望一切 运行在a.py进口时

  3. 要解决第2点的问题, Python 允许您使用条件检查

  4. __name__全部为隐含变量.py模块 :

  • 何时a.pyimported, 数值__name__联 联 年 月 日 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 月 日 月 的 月 月 月 日 月 月 日 月 的 月 月 月 月 日 月 月 月a.py模块设置为文件名 "a"
  • 何时a.py直接使用“python a.py" , " 价值__name__设置为字符串__main__
  1. 基于 Python 如何设置变量的机制__name__每个模块的每个模块, 您知道如何达到第3点吗 ? 答案相当简单, 对吧? 使用如果条件 :if __name__ == "__main__": // do A
  • 时当时python a.py将运行此部件// do A
  • import a跳过此部分// do A
  1. 你甚至可以放__name__ == "a"取决于你的功能需要,但很少

Python在4点是特别的 重要的事!

我在这页的答案中读了这么多。我想说,如果你知道这件事的话,你肯定能理解那些答案, 否则,你还是很困惑。