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

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

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


当前回答

我们看看__name__ == '__main__':经常

它检查一个模块是否正在导入 。

换言之,在if只有在代码直接运行时才执行此块。这里directly中指not imported.

让我们看看它用一个简单的代码 来打印模块的名称做什么:

# test.py
def test():
   print('test module name=%s' %(__name__))

if __name__ == '__main__':
   print('call test()')
   test()

如果我们直接通过python test.py,模块名称是__main__:

call test()
test module name=__main__

其他回答

if __name__ == "__main__":无法在导入文件时运行不需要的代码 。

例如,这是test1.pyif __name__ == "__main__"::

# "test1.py"

def hello()
    print("Hello")

hello()

还有test2.py进口test1.py:

# "test2.py"

import test1 # Here


然后,当奔跑的时候,test2.py, 1个Hello打印是因为不需要的代码hello()test1.py同时运行 :

python test2.py
Hello

当然,你可以打给test1.hello()test2.py:

# "test2.py"

import test1

test1.hello() # Here

然后,当奔跑的时候,test2, Hello已经打印 :

python test2.py
Hello
Hello

现在,添加if __name__ == "__main__":test1.py并放hello()在其之下:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()

这是test2.py:

# "test2.py"

import test1

test1.hello()

然后,当奔跑的时候,test2.py, 只有一个Hello打印是因为if __name__ == "__main__":防止要运行不需要的代码hello()何时test2.py进口进口进口test1.py:

python test2.py
Hello

此外,是否test1.py拥有if __name__ == "__main__":或非:

# "test1.py"

def hello()
    print("Hello")

if __name__ == "__main__":
    hello()
# "test1.py"

def hello()
    print("Hello")

hello()

一个Hello运行时正确打印test1.py:

python test1.py
Hello

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将不执行。

当执行 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!

创建以下两个文件 :

# a.py

import b
# b.py

print("__name__ equals " + __name__)

if __name__ == '__main__':
    print("if-statement was executed")

现在每个文件都单独运行 。


运行中python a.py:

$ python a.py
__name__ equals b

何时a.py执行时,它导入模块b。这导致所有代码都在里面b要运行。 Python 设置globals()['__name__']和在b模块的模块到模块名称,b.


运行中python b.py:

$ python b.py
__name__ equals __main__
if-statement was executed

仅在文件b.py已执行, Python 设置globals()['__name__']在此文件中的文件中"__main__"因此,if用于True这一次。

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

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

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

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