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

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

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


当前回答

短答

保护用户在无意时不意外地引用剧本的锅炉牌代码。 当剧本遗漏了警卫时, 这里有一些常见的问题:

  • 如果您在另一个脚本中导入了无保护脚本(例如 ) 。import my_script_without_a_name_eq_main_guard))),后一脚本将触发前一脚本运行导入时使用第二脚本命令行参数。这几乎总是一个错误。

  • 如果您在无警卫的脚本中有自定义类, 并将其保存为泡菜文件, 那么在另一部脚本中拆开它就会触发无警卫的脚本的输入, 与前一颗子弹中描述的相同问题 。

长答答

为了更好地了解为什么和如何看待这一问题,我们需要退一步,了解Python初始化脚本的方式,以及这如何与其模块输入机制相互作用。

Python 译员读到源文件时,

  • 它设置了几个特殊的变量,比如__name__,然后

  • 它执行文件中发现的所有代码。

让我们看看这是怎么回事 和它与你的问题有什么关系__name__我们总是在 Python 脚本中看到检查。

代码样本

让我们使用略微不同的代码样本来探索导入和脚本是如何工作的。 假设以下是在一个名为foo.py.

# Suppose this is foo.py.

print("before import")
import math

print("before function_a")
def function_a():
    print("Function A")

print("before function_b")
def function_b():
    print("Function B {}".format(math.sqrt(100)))

print("before __name__ guard")
if __name__ == '__main__':
    function_a()
    function_b()
print("after __name__ guard")

特殊变量

当 Python 口译员读取源文件时, 它首先定义了几个特殊变量。 在这种情况下, 我们关心的是__name__变量。

当您的模块是主程序时

如果您正在将模块(源文件)作为主程序运行,例如 。

python foo.py

口译员将指定硬编码字符串"__main__"会 议 日 和 排__name__变量,即:

# It's as if the interpreter inserts this at the top
# of your module when run as the main program.
__name__ = "__main__" 

当您的模块被另一个模块导入时

另一方面,假设某个其他模块是主程序, 它会导入您的模块。 这意味着在主程序或其它模块中, 有类似这样的语句, 或者是主要程序导入 :

# Suppose this is in some other main program.
import foo

译员会搜索您foo.py文件( 加上搜索其它几个变量) 执行该模块之前, 它会指定名称"foo"从导入对帐单到__name__变量,即:

# It's as if the interpreter inserts this at the top
# of your module when it's imported from another module.
__name__ = "foo"

执行模块代码

设置特殊变量后, 口译员将执行模块中的所有代码, 一次执行一个语句。 您可能想要用代码样本在侧面打开另一个窗口, 以便您可以随此解释一起执行 。

总是

  1. 它印着弦"before import"(没有引文)。

  2. 里面装满了math模块,然后将它指派给一个变量,该变量被称为math。这等于替换import math(注:__import__是在 Python 中的低级别函数, 需要字符串并触发实际导入 :

# Find and load a module given its string name, "math",
# then assign it to a local variable called math.
math = __import__("math")
  1. 它印着弦"before function_a".

  2. 它执行def键,创建函数对象,然后将该函数对象指定给一个被称作变量的变量function_a.

  3. 它印着弦"before function_b".

  4. 它执行第二个def键,创建另一个函数对象,然后将其指派到一个被调用的变量function_b.

  5. 它印着弦"before __name__ guard".

只有当您的模块是主程序时

  1. 如果您的模块是主程序, 它就会看到__name__确实设定"__main__"它调用两个功能, 打印字符串"Function A""Function B 10.0".

只有当您的模块被另一个模块导入时

  1. (取代如果您的模块不是主程序,而是被另一个程序导入,那么__name__"foo",而不是"__main__"它会跳过if语句。

总是

  1. 它会打印字符串"after __name__ guard"这两种情况都存在。

摘要摘要摘要

简而言之,这里是两个案例的印刷品:

# What gets printed if foo is the main program
before import
before function_a
before function_b
before __name__ guard
Function A
Function B 10.0
after __name__ guard
# What gets printed if foo is imported as a regular module
before import
before function_a
before function_b
before __name__ guard
after __name__ guard

为什么它这样工作?

你可能会自然而自然而自然地想知道为什么有人想要这个。.py可用于其他程序和/或模块作为模块的文件,也可以作为主要程序本身运行。例如:

  • 您的模块是一个库, 但是您想要有一个脚本模式, 它运行一些单元测试或演示 。

  • 您的模块仅作为主程序使用, 但它有一些单位测试, 以及导入的测试框架 。.py比如您的脚本文件, 并运行特殊测试功能。 您不希望它仅仅因为正在导入模块而尝试运行脚本 。

  • 您的模块大多用作主要程序, 但也为高级用户提供程序员友好的 API 。

除了这些例子之外, 在 Python 中运行脚本只是设置一些神奇变量, 并导入脚本, 这是优雅的。 “ 运行” 脚本是导入脚本模块的副作用 。

" 思想食品 " 组织

  • 问题:我能有多个__name__检查路障?回答:这样做很奇怪, 但语言不会阻止你。

  • 假设以下是foo2.py。如果你说,会发生什么呢?python foo2.py指挥线上?

# Suppose this is foo2.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters

def function_a():
    print("a1")
    from foo2 import function_b
    print("a2")
    function_b()
    print("a3")

def function_b():
    print("b")

print("t1")
if __name__ == "__main__":
    print("m1")
    function_a()
    print("m2")
print("t2")
      
  • 现在,找出如果你删除__name__检查中foo3.py:
# Suppose this is foo3.py.
import os, sys; sys.path.insert(0, os.path.dirname(__file__)) # needed for some interpreters

def function_a():
    print("a1")
    from foo3 import function_b
    print("a2")
    function_b()
    print("a3")

def function_b():
    print("b")

print("t1")
print("m1")
function_a()
print("m2")
print("t2")
  • 当作为脚本使用时, 这将做什么 ? 当作为模块导入时 ?
# Suppose this is in foo4.py
__name__ = "__main__"

def bar():
    print("bar")
    
print("before __name__ guard")
if __name__ == "__main__":
    bar()
print("after __name__ guard")

其他回答

缩略Python 主函数是一个程序的起始点。当程序运行时, Python 口译员会按顺序运行代码。主要函数只有在作为 Python 程序运行时才执行...

def main():
    print("I am in the function")

print("I am out of the function")

当运行脚本显示时 :

I am out of the function

而不是代码"我是在函数"。

这是因为我们没有宣布调用功能“if_name”主要".

使用时使用:

def main():
    print("I am in the function")

if __name__ == "__main__":
    main()

print("I am out of the function")

输出等于

I am in the function
I am out of the function

在皮顿if__name__== "__main__"允许您作为可重复使用的模块或独立程序运行 Python 文件。

当 Python 口译员读取源文件时, 它会执行其中的所有代码。 当 Python 运行“ 源文件” 作为主程序时, 它会设置特殊变量__name__要有一个值"__main__".

当您执行主函数时,它会读取if用来检查__name__等于__main__.

用于从命令行调用 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将不执行。

什么是什么东西if __name__ == "__main__":是吗?

__name__是一个全球变量(在 Python 中,在模块模块级别)存在于所有命名空间的所有命名空间中。它通常是模块的名称(作为str类型))

然而,作为唯一的特殊情况,无论你运行的 Python 程序,就像我的代码。 py:

python mycode.py

以其他匿名的全域命名空间被指定为'__main__'至其__name__.

因此,包括最后一行

if __name__ == '__main__':
    main()
  • 在我的代码.py脚本的结尾,
  • 当它是由 Python 进程运行的初级切入点模块时,

将会导致您的脚本有独特的定义main要运行的函数。

使用此构造的另一个好处是:您也可以将代码作为模块导入到另一个脚本中,然后运行主函数,如果且当您的程序决定:

import mycode
# ... any amount of other code
mycode.main()

当您的脚本作为命令传递给 Python 口译员时,

python myscript.py

缩进级别 0 的所有代码都得到执行。 定义的函数和类别是, 定义的, 定义明确, 但他们的代码没有运行 。 与其他语言不同, 没有main()函数自动运行 -main()函数是最高层所有代码的隐含值。

在这种情况下,最高层代码是if区块。__name__是一个内嵌变量。该变量对当前模块的名称进行评估。但是,如果模块直接运行,则该模块直接运行(如myscript.py above), then __name__而不是设置为字符串"__main__"因此,您可以测试您的脚本是直接运行的,还是通过测试输入其它东西的脚本。

if __name__ == "__main__":
    ...

如果您的脚本正在导入到另一个模块中, 它的各种函数和类定义将会导入, 并且它的顶层代码将会被执行, 但是在当时if以上条款将无法运行,因为不符合条件。作为一个基本例子,考虑以下两个脚本:

# file one.py
def func():
    print("func() in one.py")

print("top-level in one.py")

if __name__ == "__main__":
    print("one.py is being run directly")
else:
    print("one.py is being imported into another module")
# file two.py
import one

print("top-level in two.py")
one.func()

if __name__ == "__main__":
    print("two.py is being run directly")
else:
    print("two.py is being imported into another module")

如果您现在援引译员为

python one.py

产出将是

top-level in one.py
one.py is being run directly

如果你逃跑two.py代之以:

python two.py

你得到

top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly

因此,当模块one装上子弹,装上子弹__name__等于相等"one"代替"__main__".