您现在的位置是:首页 > 文章详情

为什么很多人对Python:if__name__ == __main__情有独钟

日期:2020-08-26点击:502


(转载作者请注明出处)

废话不多说,直接上货....

-----思想和方法才是灵魂-----

前期提要:
if__name__ == __main__

当Python解释器读取Python文件时,它首先设置一些特殊变量。然后,它执行文件中的代码。

这些变量之一称为__name__。

如果循序渐进地阅读本文并阅读其代码片段,您将学习如何使用 if name == "main" ,以及它为什么如此重要。

Python模块介绍

Python文件称为模块,由.py文件扩展名标识。模块可以定义函数,类和变量。

因此,当解释器运行模块时,__name__将设置变量,就像   __main__正在运行的模块是主程序一样。

但是,如果代码从另一个模块导入该模块,则该__name__  变量将设置为该模块的名称。

让我们看一个例子。创建一个名为的Python模块file_one.py并将以下顶级代码粘贴到其中:

Python file one module

print("File one __name__ is set to: {}" .format(__name__))

file_one.py 通过运行此文件,您将确切了解我们在说什么。该__name__模块的变量设置为__main__:

File one __name__ is set to: __main__

现在添加另一个名为的文件file_two.py并将此代码粘贴到其中:

Python module to import

print("File two __name__ is set to: {}" .format(__name__))

file_two.py 另外,file_one.py像这样修改代码,以便我们导入file_two模块:

Python module to execute

import file_two
print("File one __name__ is set to: {}" .format(__name__))

file_one.py file_one再次运行我们的代码将显示中的__name__变量file_one没有更改,并且仍然设置为__main__。但是现在变量__name__in file_two被设置为其模块名称,因此file_two。

结果应如下所示:

File two __name__ is set to: file_twoFile one __name__ is set to: __main__

但是file_two直接运行,您会看到其名称设置为__main__:

File two __name__ is set to: __main__

name__用于运行的文件/模块的变量将始终为__main。但是__name__,正在导入的所有其他模块的变量将被设置为其模块的名称。

Python文件命名约定 使用通常使用的方法__name__和__main__看起来像这样:

if __name__ == "__main__":

Do something here

让我们看看它在现实生活中是如何工作的,以及如何实际使用这些变量。

进行修改file_one,file_two如下所示:

file_one:

Python module to execute

import file_two
print("File one __name__ is set to: {}" .format(__name__))
if __name__ == "__main__": print("File one executed when ran directly")else: print("File one executed when imported")

file_one.py file_two:

Python module to import

print("File two __name__ is set to: {}" .format(__name__))
if __name__ == "__main__": print("File two executed when ran directly")else: print("File two executed when imported")

file_two.py 同样,在运行时,file_one您将看到程序识别出这两个模块中的哪个模块,__main__并根据我们的第一条if else语句执行了代码。

结果应如下所示:

File two __name__ is set to: file_twoFile two executed when importedFile one __name__ is set to: __main__File one executed when ran directly

现在运行file_two,您将看到该__name__变量设置为__main__:

File two __name__ is set to: __main__File two executed when ran directly

当这样的模块被导入并运行时,它们的功能将被导入,并执行顶层代码。

要查看此过程的实际效果,请将文件修改为如下所示:

file_one:

Python module to execute

import file_two
print("File one __name__ is set to: {}" .format(__name__))
def function_one(): print("Function one is executed")
def function_two(): print("Function two is executed")
if __name__ == "__main__": print("File one executed when ran directly")else: print("File one executed when imported")

file_one.py file_two:

Python module to import

print("File two __name__ is set to: {}" .format(__name__))
def function_three(): print("Function three is executed")
if __name__ == "__main__": print("File two executed when ran directly")else: print("File two executed when imported")

现在,功能已加载但无法运行。

要运行这些功能之一,请将其中的if name == "main"一部分修改为file_one如下所示:

if __name__ == "__main__": print("File one executed when ran directly") function_two()else: print("File one executed when imported")

运行时,file_one您应该看到应该是这样的:

File two __name__ is set to: file_twoFile two executed when importedFile one __name__ is set to: __main__File one executed when ran directlyFunction two is executed

另外,您可以从导入的文件运行功能。为此,将if name == “main”部分修改为file_one如下所示:

if __name__ == "__main__": print("File one executed when ran directly") function_two() file_two.function_three()else: print("File one executed when imported")

您可以期望这样的结果:

File two __name__ is set to: file_two_step_3File two executed when importedFile one __name__ is set to: __main__File one executed when ran directlyFunction two is executedFunction three is executed

现在让我们说file_two模块确实很大,有很多功能(在我们的例子中有两个),而您不想导入所有这些功能。修改file_two为如下所示:

Python module to import

print("File two __name__ is set to: {}" .format(__name__))
def function_three(): print("Function three is executed")
def function_four(): print("Function four is executed")
if __name__ == "__main__": print("File two executed when ran directly")else: print("File two executed when imported")

file_two.py 要从模块导入特定功能,请使用文件中的fromimport块file_one:

Python module to execute

from file_two_step_3 import function_three
print("File one __name__ is set to: {}" .format(__name__))
def function_one(): print("Function one is executed")
def function_two(): print("Function two is executed")
if __name__ == "__main__": print("File one executed when ran directly") function_two() function_three()else: print("File one executed when imported")

file_one.py

结论

该__name__变量有一个非常好的用例,无论您是想要一个可以作为主程序运行还是可以由其他模块导入的文件。if name == "main"导入模块时,我们可以使用块来允许或阻止部分代码运行。

当Python解释器读取文件时,将__name__变量设置为__main__好像正在运行的模块,或者将其设置为模块的名称(如果已导入)。读取文件将执行所有顶级代码,但不会执行函数和类(因为它们只会被导入)




本文分享自微信公众号 - AI科技与算法编程(kangsinx)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

原文链接:https://my.oschina.net/u/4047540/blog/4532299
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章