博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如果__name__ =='__main__':在Python中怎么办?
阅读量:2535 次
发布时间:2019-05-11

本文共 3066 字,大约阅读时间需要 10 分钟。

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.pymodule_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_mainprint("second module_name :{}".format(__name__))-bash-4.2$ python3 second_module.pymodule_name :if_mainsecond 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_mainprint("second module_name :{}".format(__name__))
-bash-4.2$ python3 second_module.pyrun from importsecond module_name :__main__-bash-4.2$-bash-4.2$ python3  if_main.pymodule_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文件可以用作独立程序或可重用模块。

翻译自:

转载地址:http://mrxzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>