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

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

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


当前回答

当您在本地交互运行 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.

其他回答

当执行 Python 文件时, 它会创建许多特殊的变量, 例如__name__变量__name__您问题的答案是:

if __name__ == "__main__":
       # Do something

这意味着,如果所执行的文件的名称作为源文件运行,并且非模块然后它会运行里面的代码。 这可以用简单的示例来证明。 创建两个 Python 文件,foo.pysecond.py。然后在foo.py,类型如下:

if __name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

和在second.py,类型如下:

import foo

if foo.__name__ == "__main__":
       print("file is not imported")
else:
       print("file is imported")

除此以外,如果你们要这样做,print(__name__)然后将打印__main__为什么?

因为文件以主要源代码,如果你愿意print(foo.__name__)将打印fuo fuo的默认值,因为__name__变量是文件的名称,默认是文件的默认名称,我的意思是,您也可以更改它。要做到这一点,只需去foo.py并做此操作的文件 :__name__ = "name"。然后当你运行文件,例如,

__name__ = "Hello, World!"
print(__name__)

然后产出将是:

Hello, World!

我认为最好打破答案的深度 和简单的话:

__name__: Python 中的每个模块都有一个特殊属性__name__。它是返回模块名称的内置变量。

__main__:与其他编程语言一样,Python也有执行的切入点,即主切入点。'__main__' 是顶层代码执行范围的名称。基本上,您有两种方法使用 Python 模块:直接作为脚本运行,或导入。当一个模块作为脚本运行时,它的脚本__name__设置为__main__.

因此,《公约》和《京都议定书》的__name__属性设置为__main__当模块作为主程序运行时,该模块作为主程序运行。否则__name__设置以包含模块的名称。

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

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

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

if __name__ == "__main__"是在脚本从命令行运行时运行的部分,使用类似命令python myscript.py.

您可以查看特殊变量__name__使用此简单示例 :

创建创建文件1. py

if __name__ == "__main__":
    print("file1 is being run directly")
else:
    print("file1 is being imported")

创建 *file2。平平

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")

执行文件2. py

Output:

file1 is being imported
__name__ from file1: file1
__name__ from file2: __main__
file2 is being run directly