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

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

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


当前回答

创建文件,a.py , a.py , a.py , a.py , a.py , a.py , a.py , a.py:

print(__name__) # It will print out __main__

__name__始终等于__main__当该文件为直接直接运行显示此为主文件 。

创建另一个文件,b.py b.py,在同一目录中:

import a  # Prints a

运行它,它会打印a,即哪个文件的名称已导入.

所以,为了展示同一文件的两种不同行为,这是一个常用的把戏:

# Code to be run when imported into another python file

if __name__ == '__main__':
    # Code to be run only when 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

创建以下两个文件 :

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

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

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

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

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

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