温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python的if__name__ == __main__有什么作用

发布时间:2021-11-26 11:43:30 来源:亿速云 阅读:101 作者:iii 栏目:大数据

这篇文章主要讲解了“Python的if__name__ == __main__有什么作用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python的if__name__ == __main__有什么作用”吧!

前期提要:
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")

感谢各位的阅读,以上就是“Python的if__name__ == __main__有什么作用”的内容了,经过本文的学习后,相信大家对Python的if__name__ == __main__有什么作用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI