为什么很多人对Python: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_two
File 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_two
File two executed when imported
File 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_two
File two executed when imported
File one __name__ is set to: __main__
File one executed when ran directly
Function 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_3
File two executed when imported
File one __name__ is set to: __main__
File one executed when ran directly
Function two is executed
Function 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源创计划”,欢迎正在阅读的你也加入,一起分享。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
让音乐伴随你左右-Milvus 在丸音的应用
✏️ 作者介绍: Jason,不亦乐乎科技算法工程师 陈室余,Zilliz 数据工程师 | 背景 丸音 APP 是一款基于 AI 音乐创作的音乐分享社区。我们希望通过丸音,让更多喜欢音乐的人能轻松地进行音乐创作,在丸音拥有属于你自己的音乐! 丸音的库中有用户上传的海量音乐。我们的首要任务是如何基于用户的历史行为,从海量音乐中筛选出用户感兴趣的音乐。关于推荐系统模型,我们考虑了最经典的基于用户的协同过滤(User-based CF)和基于项目的协同过滤(Item-based CF): 基于用户的协同过滤:用相似统计的方法得到具有相似爱好或者兴趣的相邻用户。有了最近邻用户集合,就可以对目标用户的兴趣进行预测,产生推荐结果。 基于项目的协同过滤:由亚马逊推出的 Item-to-Item (I2I) CF 推荐系统广为人知。该算法通过计算项目之间的相似性来代替计算用户之间的相似性,所依据的基本假设是 “能够引起用户兴趣的项目,必定与其之前评分高的项目相似”。 基于用户的协同过滤在用户总数较多的情况下会导致漫长的计算时间,同时考虑到产品特性,我们打算使用 I2I 实现音乐推荐。但由于目前并没有太...
- 下一篇
我参与了两个接近100k+star的开源项目!聊聊开源项目贡献指南
给 SkyWalking 以及 JavaGuide 项目贡献后的总结 JavaGuide: https://github.com/Snailclimb/JavaGuide SkyWalking: https://github.com/apache/skywalking 1. 本地开发 以 SkyWalking 举例。在本地编译源码前,先查看相关的文档:https://github.com/apache/skywalking/blob/v8.0.1/docs/en/guides/How-to-build.md 。大致了解后,我们就可以开始操作了。 在 Github 上 fork 你想要贡献的项目 接着在本地拉取自己的项目: git clone --recurse-submodules https://github.com/$Name/skywalking.git 这是因为 SkyWalking 它包含了子仓库,因此加入了 --recurse-submodules 参数,它可以把主仓库和子仓库源码都同时拉取。 代码拉到本地后,接着我们使用 idea 打开该项目。但是可能我们网络不够给力或有...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS关闭SELinux安全模块
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Docker使用Oracle官方镜像安装(12C,18C,19C)