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

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

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


当前回答

缩略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 主函数是一个程序的起始点。当程序运行时, 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__.

简言之:

您所看到的代码下面的代码if __name__ == "__main__":只有当您的 Python 文件被执行时才会被调用 。python example1.py

但是,如果您想要导入您的 Python 文件example1.py以模块的形式使用另一个 Python 文件, 例如example2.py下的代码if __name__ == "__main__":将不会运行或产生任何效果。

当您在本地交互运行 Python 时__name__变量的变量被指定值__main__同样,当您从命令行执行 Python 模块时,而不是将它导入到另一个模块中时,它__name__属性的属性被指定值__main__,而不是模块的实际名称。这样,模块可以查看自己的__name__用于确定它们是如何使用的值, 无论是作为对其它程序的支持还是作为从命令行执行的主要应用程序。 因此, 在 Python 模块中, 下列语系非常常见 :

if __name__ == '__main__':
    # Do something appropriate here, like calling a
    # main() function defined elsewhere in this module.
    main()
else:
    # Do nothing. This module has been imported by another
    # module that wants to make use of the functions,
    # classes and other useful bits it has defined.

如果 Python 口译员正在运行一个特定模块,那么__name__全局变量将拥有值"__main__":

def a():
    print("a")

def b():
    print("b")

if __name__ == "__main__":

        print ("you can see me")
        a()
else:

        print ("You can't see me")
        b()

当您运行此脚本时, 它会打印 :

you can see me
a

如果您导入此文件, 请说A要到文件的文件B,然后执行文件B时当时if __name__ == "__main__"文件中的文件A成为假假,所以它指纹:

You can't see me
b

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