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

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

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


当前回答

您可以将文件作为脚本脚本脚本和(或)可导入可导入模块.

vibbo.py (一个称为模块的模块)fibo)

# Other modules can IMPORT this MODULE to use the function fib
def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

# This allows the file to be used as a SCRIPT
if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

参考:https://docs.python.org/3.5/tutorial/modules.html

其他回答

创建以下两个文件 :

# 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这一次。

当您的脚本作为命令传递给 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__".

"如果"允许您选择使用 python 文件作为模块或脚本

  1. 如果(如果)名称名称名称名称名称=='主要`然后 python 文件被用作脚本 - 在这个语句下有逻辑值 。
  2. 如果名称名称名称名称名称 != '主要只有此 python 文件的函数/变量可用于其他 python 脚本enter image description here

系统( 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))

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

希望这能帮上忙

缩略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__.