如果__name__ =='__main__':在Python中怎么办?

In order to understand the details of __name__ variable and the if condition, let us go through a simple exercise. Run a simple python file with just the following lines and run the file as python3 code,

为了了解__name__变量和if条件的详细信息,让我们进行一个简单的练习。 仅使用以下几行运行一个简单的python文件,并将其作为python3代码运行,

print("module_name :{}".format(__name__))
-bash-4.2$ python3 if_main.py
module_name :__main__
-bash-4.2$

In the above example, the output of the program states that the variable __name__ has a value of __main__. So, what happens is, in the background when python runs a file it goes through before it even runs any code, it sets a few special variables and 'name' is one of those special variables and when python runs the code it sets the '__name__' variable to '__main__'.

在上面的示例中,程序的输出表明变量__name__的值为__main__ 。 因此,发生的事情是,在后台运行python的文件时,甚至在运行任何代码之前都要经过它,它会设置一些特殊变量,而“ name”是这些特殊变量之一,而当python运行代码时,它将设置' __name__'变量为'__main__'

We can also import the modules, and when the modules are imported the variable __name__ is set to name the file.

我们还可以导入模块,并且在导入模块时,将变量__name__设置为文件名称。

Example:

例:

Create a file called second_module.py, and in second_module.py add the following lines and run the file.

创建一个名为second_module.py的文件,并在second_module.py中添加以下行并运行该文件。

import if_main
print("second module_name :{}".format(__name__))
-bash-4.2$ python3 second_module.py
module_name :if_main
second module_name :__main__
-bash-4.2$

In above example, we see that the imported file prints the file name and the second_module prints __main__, the reason being, the imported file is not run directly by python instead it is an imported file and hence the variable __name__ is set to the file name and the second_module is directly run by python and hence the variable __name__ is set to the __main__. Now returning to the subject of what does if __name__ == '__main__' do?

在上面的示例中,我们看到导入的文件打印了文件名, second_module打印了__main__ ,原因是,导入的文件不是直接由python运行,而是导入的文件,因此变量__name__被设置为文件名并且second_module直接由蟒运行并且因此可变__name__被设置为__main__。 现在回到__name__ =='__main__'怎么办的主题?

__name__ ==“ __main__”怎么办? (What does if __name__ == "__main__": do?)

When the above condition is checked, it is to assert if the file is directly run by python or is it being imported. The following example explains the usage of the if condition,

选中以上条件后,将声明该文件是直接由python运行还是正在导入。 以下示例说明了if条件的用法,

File 1 : if_main.py

文件1:if_main.py

def main():
print("module_name :{}".format(__name__))
if __name__ == "__main__":
main()
else:
print("run from import")

File 2 : second_module.py

文件2:second_module.py

import if_main
print("second module_name :{}".format(__name__))

-bash-4.2$ python3 second_module.py
run from import
second module_name :__main__
-bash-4.2$
-bash-4.2$ python3  if_main.py
module_name :__main__
-bash-4.2$

优点 (Advantages)

  1. The reason to use this is ensuring to run some code only when it is directly run from the main file and some code will be executed only when it is imported.

    使用此命令的原因是确保仅在直接从主文件运行时才运行某些代码,并且仅在导入时才执行某些代码。

  2. The python file can be used either as a standalone program or a reusable module.

    python文件可以用作独立程序或可重用模块。

翻译自: https://www.includehelp.com/python/what-does-if__name__-__main__-do.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378708.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Eclipse C/C++开发环境搭建

1 Eclipse的安装 到http://java.sun.com/j2se/1.5.0/download.jsp 下载JRE安装; 到http://eclipse.org下载Eclipse安装。(这儿可以下载Java版本的,也可以下载C/C 版本的) 2 对于下载的Java版本或着只下载Eclipse IDE的&#xff0c…

微机原理——寻址方式总结

一、操作数的寻址方式 立即寻址方式 格式: 操作码 数字表达式(将数据送入寄存器中) 源操作数可以是8位也可以是16位。 MOV AH, F5H (字节操作) F5H称为立即数(8位操作数) MOV AL, 8AH (字节操作) 8AH称为…

15-轮廓检测

边缘是零零散散的,而轮廓是一个整体 cv2.findContours(img,mode,method) img:输入图像对象名称 mode:轮廓检索模式 RETR_EXTERNAL:只检索最外面的轮廓 RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中…

抛硬币仿真实验java_探索HyperLogLog算法(含Java实现)

引言HyperLogLog算法经常在数据库中被用来统计某一字段的Distinct Value(下文简称DV),比如Redis的HyperLogLog结构,出于好奇探索了一下这个算法的原理,无奈中文资料很少,只能直接去阅读论文以及一些英文资料,总结成此文…

kotlin键值对数组_Kotlin程序以升序对数组进行排序

kotlin键值对数组Given an array, we have to sort its elements in ascending order. 给定一个数组,我们必须按升序对其元素进行排序。 Example: 例: Input:arr [10, 20, 5, 2, 30]Output:sorted array (Ascending Order): [2, 5, 10, 20, 30]在Kotl…

微机原理——总线和时序

前提 8088有两个组态: 最大组态和最小组态,通过引脚MN/MX*的电平决定组态。(*表示低电平有效) 两种组态没有本质区别。 8088的引脚: 引脚可分为下面几种类别: 1、数据和地址引脚 2、读写控制引脚 3、中断…

PHP站内搜索:多关键字查找,加亮显示

1、SQL语句中的模糊查找LIKE条件一般用在指定搜索某字段的时候, 通过"% _" 通配符的作用实现模糊查找功能,通配符可以在前面也可以在后面或前后都有。搜索以PHP100开头: SELECT * FROM teble WHERE title LIKE PHP100% 搜索以PHP100结束&…

16-模板匹配

cv2.matchTemplate(img,template,cv2.TM_SQDIFF) 参数一:原图图像对象名称 参数二:模板图像对象名称 参数三:差别程度的计算方法(六选一推荐使用带归一化的) 模板匹配和卷积原理很像,模板从原图像上从原点开始滑动,计…

对MySQL性能影响关系紧密的五大配置参数

以下的文章主要是对MySQL性能影响关系紧密的五大配置参数的介绍,我前几天在相关网站看见对MySQL性能影响关系紧密的五大配置参数的资料,觉得挺好,就拿出来供大家分享,望你能有所收获。(一)连接 连接通常来自Web服务器,…

JAVA安装作用_jdk安装配置及其作用

2.安装好了就是去配置路径了,我的是win7系统,步骤如下:桌面上的计算机右击-》高级系统设置—》环境变量-》系统变量-》新建一共要新建三个变量JAVA_HOME,PATH和CLASSPATH1>JAVA_HOME:(这么写为了方便以后可能改动jdk的安装路径&#xff0c…

用C#开发Windows应用程序

To develop windows application, we need to using studio and follow some steps: 要开发Windows应用程序 ,我们需要使用studio并遵循一些步骤: Step 1) First of all we launch visual studio. 步骤1)首先,我们启动Visual Studio。 Ste…

图像分割——基于二维灰度直方图的阈值处理

前言 像素灰度值仅仅反映了像素灰度级的幅值大小,并没有反映出像素与邻域的空间相关信息。 二维灰度直方图的概念 二维灰度直方图:像素的灰度值分布和邻域的平均灰度值分布构成的二维直方图 二维直方图的值N(i,j) 。其中,if(x,y) 图像(x,y…

多维角度聊聊结对编程

在敏捷软件开发的各种实践中,结对编程(Pair Programming,下文简称Pair)是特别有争议的。Pair有一个特点,那就是还没有进行过任何Pair实践前,你很可能对它已经有了“喜欢” 或者是“讨厌”的印象。如果有人问…

17-直方图

直方图 何为直方图?没那么高大上,其实就是二维统计图。每个照片都是有像素点所组成,当然也是[0,255],直方图就是统计每个值所对应的像素点有几个。 直方图横坐标表示0-255这些像素点值;纵坐标表示对应像素点值的个数有…

java求水电费_java水电费管理系统

每天记录学习,每天会有好心情。*^_^*今天和一个朋友共同完成了一个基于web的java水电费管理系统项目,我们在开发时选用的框架是SSM(MYECLIPSE)框架。我这个朋友知识有限,只会这个框架,哈哈,都是为了方便他。和往常一样…

zemax微透镜阵列示例_阵列反向! Ruby中的示例方法

zemax微透镜阵列示例阵列反向! 方法 (Array reverse! Method) In this article, we will study about Array.reverse! method. You all must be thinking the method must be doing something related to reversing certain elements as we have done in the case o…

Opencv实战【1】人脸检测并对ROI区域进行部分处理(变身乔碧萝!!!)

步骤: 1、利用Opencv自带的分类器检测人脸 预备知识:Haar特征分类器 Haar特征分类器就是一个XML文件,该文件中会描述人体各个部位的Haar特征值。包括人脸、眼睛、嘴唇等等。 Haar特征分类器存放地址: (找自己的安装…

【黑马甄选离线数仓day10_会员主题域开发_DWS和ADS层】

day10_会员主题域开发 会员主题_DWS和ADS层 DWS层开发 门店会员分类天表: 维度指标: 指标:新增注册会员数、累计注册会员数、新增消费会员数、累计消费会员数、新增复购会员数、累计复购会员数、活跃会员数、沉睡会员数、会员消费金额 维度: 时间维度&#xff08…

iPad和iPhone的app图标尺寸、用途、设置方法

下面是在iPhone专用程序、iPad专用程序和通用程序中使用图标文件的指导,由译言网翻译自苹果官方文档。原文 http://article.yeeyan.org/view/395/100567 注意:图标是你的程序包所必需的组成部分。如果你没有提供程 序所需的各种尺寸的图标,系…

18-傅里叶变化

以时间为参照就是时域分析,当然时间是动态变化的 而傅里叶变换是以频域为基准的,不用关心动态变化,只关心做了多少次而已,次数,频率 傅里叶说过,任何一个周期函数都可以用正弦函数堆叠起来形成。强吧&#…