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

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

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


当前回答

假设我在维基百科上为网上剪贴写功能和课程。 当然,这也许不是一个好例子。

我想在另一个程序里使用这些函数 而不改写它

嗯,我进口了它们, 但是在文件的结尾,我放__name__ == '__main__'

当我们importa 模块,其中的所有代码都从开始到结束执行。但是当它达到条件时,它不会运行调乐曲 func, 复真2c2cfluc2等,这是维基百科__scrape__.

那么,在全球范围内,Python(Python)__name__定义为'__main__'用于当前程序 。

当我们import模块,它定义为当前程序和当前程序的名称空间中的变量。__name__'__main__':

文件文件文件测试.py

def func():
    # Do something
    pass

def func2():
    # Do something
    pass

print('The program name is set to ', globals()['__name__'])

if __name__=='__main__':
    # In the current program, __name__ is equal to '__main__'
    func('https://www.wikipedia.org')
    func2('https://www.wikipedia.org')
    # Or do more jobs

import test1
print('inside of current program')
print('name is current program', __name__)
print(globals()['test1'])
test1.func('another site')
test1.func2('another site')

Output

inside of test 1
name of program is set to test1
end of module
inside of current
__main__
<module 'test1' from 'C:\\users\\ir\\appdata\\local\\programs\\python\\python38\\lib\\test1.py'>

其他回答

下代码的代码if __name__ == '__main__':仅仅如果将模块作为脚本引用,则执行该模块。

例如,考虑以下模块my_test_module.py:

# my_test_module.py

print('This is going to be printed out, no matter what')

if __name__ == '__main__':
    print('This is going to be printed out, only if user invokes the module as a script')

第一种可能性:进口my_test_module.py在另一个模块中

# main.py

import my_test_module

if __name__ == '__main__':
    print('Hello from main.py')

如果你现在祈祷main.py:

python main.py

>> 'This is going to be printed out, no matter what'
>> 'Hello from main.py'

请注意,只有最高层print()语中的语中my_test_module执行。


第二种可能性:my_test_module.py作为脚本

现在,如果你现在跑my_test_module.py作为 Python 脚本, 两者print()将执行语句:

python my_test_module.py

>>> 'This is going to be printed out, no matter what'
>>> 'This is going to be printed out, only if user invokes the module as a script'

更全面的解释,可读作:什么是什么东西if __name__ == '__main__'在 Python 中做.

所有答案都大致解释了它的功能。但我将举一个例子来说明它的用法,这可能有助于进一步澄清这个概念。

假设你有两个Python文件,A.py和B.py。a.py进口进口进口b.py。我们运行a.py文件, 在哪里import b.py代码先执行。在其余a.py代码运行,文件中的代码b.py必须完全运行。

在b.py 代码中,有一些代码是那个文件的独有代码b.py我们不想要任何其他文件(不包括b.py已导入 b. py 文件来运行 。

这就是这行代码检查。如果它是主文件(即,b.py运行代码,在这种情况下它不是代码 (a.py是正在运行的主文件),然后只有代码才能被执行。

如果 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

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

简而言之,你需要知道几点:

  1. import a动作实际上运行所有可以运行的a.py,意指每一行a.py

  2. 因为第1点,你可能不希望一切 运行在a.py进口时

  3. 要解决第2点的问题, Python 允许您使用条件检查

  4. __name__全部为隐含变量.py模块 :

  • 何时a.pyimported, 数值__name__联 联 年 月 日 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 日 月 月 月 日 月 月 日 月 月 月 日 月 的 月 月 月 日 月 月 日 月 的 月 月 月 月 日 月 月 月a.py模块设置为文件名 "a"
  • 何时a.py直接使用“python a.py" , " 价值__name__设置为字符串__main__
  1. 基于 Python 如何设置变量的机制__name__每个模块的每个模块, 您知道如何达到第3点吗 ? 答案相当简单, 对吧? 使用如果条件 :if __name__ == "__main__": // do A
  • 时当时python a.py将运行此部件// do A
  • import a跳过此部分// do A
  1. 你甚至可以放__name__ == "a"取决于你的功能需要,但很少

Python在4点是特别的 重要的事!

我在这页的答案中读了这么多。我想说,如果你知道这件事的话,你肯定能理解那些答案, 否则,你还是很困惑。