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

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

如果你试图结束一个问题 如果你想要结束一个问题 在那里有人应该应该 使用这个学说,而不是,是不是, 考虑关闭作为 重复的为什么Python在我进口时 运行我的模块? 我该如何阻止它?。而对于某些问题,如果有人只是没有调用任何函数,或者错误地期望指定函数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将不执行。

其他回答

考虑:

print __name__

上述产出是__main__.

if __name__ == "__main__":
  print "direct method"

上述陈述属实,印有指纹“直接方法”假想他们是否将这门课 输入到另一门课上,它不会打印“直接方法”因为在进口时,它将设置__name__ equal to "first model name".

下代码的代码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 中做.

if __name__ == "__main__"是在脚本从命令行运行时运行的部分,使用类似命令python myscript.py.

在解释之前if __name__ == '__main__'了解什么是__name__和它的作用。

什么是__name__?

__name__是 a 是达德阿里亚斯Name- 可被视为全球变量(可从模块中获取),工作方式与global.

它是一个字符串(如上所述,全球范围),如下文所示:type(__name__)(年均(年均)<class 'str'>),而两者都是一个内在的标准,Python 3Python 2版本的版本 。

何处处

它不仅可以用于脚本,还可以在口译和模块/软件包中找到。

解释器 :

>>> print(__name__)
__main__
>>>

脚本 :

测试_ 文件. py:

print(__name__)

导致__main__

模块或包件 :

一些文件. py:

def somefunction():
    print(__name__)

测试_ 文件. py :

import somefile
somefile.somefunction()

导致somefile

请注意,如果在包件或模块中使用,__name__选择文件的名称。没有给实际模块或软件包路径的路径,而是有它自己的 DunderAlias 路径。__file__,允许这样做。

你应该看到,在哪里__name__,它是主文件(或程序)的始终始终返回返回__main__,如果它是模块/软件包,或者正在从其他 Python 脚本中运行的东西,将返回其来源文件的名称。

实践实践实践

变数意味着它的价值能够被覆盖 (“ can” 并不意味着“sh') , 覆盖__name__将会导致缺乏可读性。 所以无论出于什么原因都不要这样做。 如果您需要变量定义新的变量, 请定义新的变量 。

人们总是认为,人们总是认为,__name__待 待 待 待 待 待 待 待__main__或文件的名称。 再次更改此默认值将会造成更多的混乱, 从而导致问题更加严重 。

示例:

>>> __name__ = 'Horrify' # Change default from __main__
>>> if __name__ == 'Horrify': print(__name__)
...
>>> else: print('Not Horrify')
...
Horrify
>>>

一般认为良好做法包括:if __name__ == '__main__'在脚本中。

现在回答if __name__ == '__main__':

现在我们知道__name__(b) 使情况更加明确:

A 音 Aif是一个流量控制语句,其中含有如果给定值是真实的将执行的代码块块。我们已经看到__name__中可选择其中之一__main__或从它导入的文件名 。

这意味着如果__name__等于__main__然后,文件必须是主文件,而且必须实际运行(或翻译),而不是将模块或软件包导入到脚本中。

如果确实__name__值的数值__main__然后那块代码里的任何东西 都会被执行

这告诉我们, 如果正在运行的文件是主文件( 或者您正在直接从翻译器中运行) , 那么该条件必须执行。 如果它是软件包, 那么它不应该, 而值将不会是__main__.

模块模块

__name__也可以用于模块中定义模块名称的模块

变式

此外,还可能采取其他不太常见但有用的措施,包括:__name__有些我会在这里展示:

仅在文件是一个模块或软件包时才执行

if __name__ != '__main__':
    # Do some useful things 

如果文件是主文件,则运行一个条件;如果不是主文件,则运行另一个条件

if __name__ == '__main__':
    # Execute something
else:
    # Do some useful things

您也可以使用它在软件包和模块上提供可运行的帮助功能/用途,而不必精密使用图书馆。

它还允许模块作为主脚本从命令行运行,这也非常有用。

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将不执行。