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

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

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


当前回答

系统( Python 口译员) 提供源文件( 模块) 的变量。 您可以随时获得源文件的值, 因此, 让我们集中关注- 名称 - 名称 - 名称变量/属性 :

当 Python 装入源代码文件时, 它会执行其中的所有代码 。 (注意它不会调用文件中定义的所有方法和函数, 但是它会定义它们 。 )

在口译员执行源代码文件之前,它为该文件定义了几个特殊变量;- 名称 - 名称 - 名称是 Python 为每个源代码文件自动定义的特殊变量之一。

如果 Python 正在装入此源代码文件作为主程序( 即您运行的文件) , 它会设置特殊- 名称 - 名称 - 名称此文件有值的变量"... main..." "... main...".

如果这是从另一个模块导入的,- 名称 - 名称 - 名称将设定为该模块的名称。

所以,在你的部分例子中:

if __name__ == "__main__":
   lock = thread.allocate_lock()
   thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
   thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

意思是代码块:

lock = thread.allocate_lock()
thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))

只有当您直接运行模块模块时才会执行;如果另一个模块正在调用/导入代码块,代码块将不执行,因为- 名称 - 名称 - 名称" 等于 " 等于 " 等于 " 等于 " 等于 " 等于 " 等于 " 等于 " 等于 " 等于 " 等于 "主要" 在此特定情况下。

希望这能帮上忙

其他回答

您可以查看特殊变量__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

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

我们看看__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__

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

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

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

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

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

也可以以几种方式写成。

def some_function_for_instance_main():
    dosomething()


__name__ == '__main__' and some_function_for_instance_main()

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

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